80,349
社区成员
发帖
与我相关
我的任务
分享
public class MainActivity extends Activity implements OnClickListener
{
……
Boolean bFlag = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
{
Log.i("handle", "Landscape!");
setContentView(R.layout.activity_main_land);
}
else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
{
Log.i("handle", "Portrait!");
setContentView(R.layout.activity_main);
}
……
/***************************下面是我尝试在代码中改背景,这一部分删除后程序可以正常运行*****************************/
Resources resources = getBaseContext().getResources();
Log.v("handle","ini resources!");//logcat显示了此行
Drawable btnDrawable = resources.getDrawable(R.drawable.green);;
Log.v("handle","ini drawable!");//green是一个jpg图片,如果通过xml可以替换//logcat显示了此行
LinearLayout ll = (LinearLayout) findViewById(R.layout.activity_main);
Log.v("handle","ini linearlayout!");//activity_main是一个Linearlayout布局//logcat显示了此行
btnDrawable = resources.getDrawable(R.drawable.drops);
Log.v("handle","ini btndrawable!");//drops是一个jpg图片,如果通过xml可以替换//logcat显示了此行
ll.setBackground(btnDrawable);//Color.PINK//运行到这里出错了……
//ll.setBackgroundColor(Color.MAGENTA);//这样也出错了……
Log.v("handle","ini background!");//没有显示这一行,说明上面一句出错了: java.lang.NullPointerException
/***************************上面部分删除后程序可以正常运行*****************************/
……
}
}
public class MainActivity extends Activity{
Resources resources = getBaseContext().getResources();//oncreate前获取是空的,出错
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
上面这么简单的例子都报错。。可见不能在oncreate之前使用this,因为都没初始化。这就是你那空指针的问题
public static Drawable setMyDrawable(Context context){
Resources resources = context.getResources();
Drawable btnDrawable =resources.getDrawable(R.drawable.ic_launcher);
return btnDrawable;
}
这样你就获得了一个drawable
这个是临时写的,你可以考虑下完善它。[/quote]
Contest我刚查了一些资料,但是觉得没有实例难以理解,在我这个问题中——限于水平——你给出的这个静态方法,是放在SetBackground.java类中吗?具体如何调用?但我觉得,问题不是在这儿吧,系统提示出错是 空指针,但我没觉得哪儿数据传递有问题呀?
public static Drawable setMyDrawable(Context context){
Resources resources = context.getResources();
Drawable btnDrawable =resources.getDrawable(R.drawable.ic_launcher);
return btnDrawable;
}
这样你就获得了一个drawable
这个是临时写的,你可以考虑下完善它。
public class MainActivity extends Activity{
Resources resources = getBaseContext().getResources();//oncreate前获取是空的,出错
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
上面这么简单的例子都报错。。可见不能在oncreate之前使用this,因为都没初始化。这就是你那空指针的问题[/quote]
呃,前辈,我的resources赋值是在onCreate之后,只不过是在MainActivity的界面中,调取SetBackground界面中的方法造成了空指针问题,如果直接在MainActivity中自己调用,就没有这个错误……
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="1dp"
android:layout_marginRight="1dp"
……
线性布局没错吧?
第一个error是:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.TT.tomatotimer/com.TT.tomatotimer.MainActivity}: java.lang.NullPointerException
public class SetBackground extends Activity
{
public int iFlag = 1;
Resources resources = getBaseContext().getResources();
Drawable btnDrawable = resources.getDrawable(R.drawable.green);;
LinearLayout ll = (LinearLayout) findViewById(R.id.activity_main);//还是这里除了问题
btnDrawable = resources.getDrawable(R.drawable.drops);//仍然是此处警告空指针错误,也就是说上面的ll没有赋值正确。
SetBackground()
{
// ll = (LinearLayout) findViewById(R.id.activity_main);
}
}
[/quote]
有问题。。你这个是继承一个activity,你不能在oncreate之前进行
findViewById或者使用资源文件。会找不到的。[/quote]
额,这怎么办我想试图将程序模块化,以后我想把 设置背景、设置音量都放在一个专门的Setting.java中,现在连设置背景都不能单独搞一个类,以后主函数可真冗长的……
另,我的setBackground.java必须extends acticity,要不然
Resources resources = getBaseContext().getResources();
Drawable btnDrawable = resources.getDrawable(R.drawable.green);;
这两行会出错,错误在于,找不到getBaseContext()。而继承Activity,就有了……木有办法呀……
public class SetBackground extends Activity
{
public int iFlag = 1;
Resources resources = getBaseContext().getResources();
Drawable btnDrawable = resources.getDrawable(R.drawable.green);;
LinearLayout ll = (LinearLayout) findViewById(R.id.activity_main);//还是这里除了问题
btnDrawable = resources.getDrawable(R.drawable.drops);//仍然是此处警告空指针错误,也就是说上面的ll没有赋值正确。
SetBackground()
{
// ll = (LinearLayout) findViewById(R.id.activity_main);
}
}
[/quote]
有问题。。你这个是继承一个activity,你不能在oncreate之前进行
findViewById或者使用资源文件。会找不到的。