本帖最后由 weiran2009 于 2014-12-23 11:37:47 编辑

解决方案 »

  1.   

    把logcat也贴出吧。
    LinearLayout ll = (LinearLayout) findViewById(R.layout.activity_main)??映射错了吧
      

  2.   

    空指针,输出一下btnDrawable的值看看
      

  3.   

    隐射错是什么概念?
    R.layout.activity_main:<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
      

  4.   

    更改和输出如下:
    Drawable btnDrawable = resources.getDrawable(R.drawable.green);;
    Log.v("handle","ini drawable!");//green是一个jpg图片,如果通过xml可以替换//logcat显示了此行
    Log.v("handle", "after green :btnDrawable: " + btnDrawable);

    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显示了此行
    Log.v("handle", "after drops :btnDrawable: " + btnDrawable);
    ————————————————————————————加了2句,输出分别为:
    after green :btnDrawable: android.graphics.drawable.BitmapDrawable@41857b58
    after drops :btnDrawable: android.graphics.drawable.BitmapDrawable@417ea590————————————————————————————————
    有什么其它更改背景的好方法吗?
      

  5.   

    1楼正解<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/act_main_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="1dp"
        android:layout_marginRight="1dp"
    LinearLayout ll = (LinearLayout) findViewById(R.id.act_main_container);代码中没有看到R.layout.activity_main这个id。
      

  6.   

    不好意思,标签弄错了,<span style="color: #FF0000;">那句不用看,主要是你要设置的LinearLayout加上 android:id,然后通过findViewById就行了
      

  7.   

    阁下意思是:
    LinearLayout ll = (LinearLayout) findViewById(R.id.activity_main);
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/activity_main"                                        --------这里添加了
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="1dp"                          
        android:layout_marginRight="1dp"但是
    LinearLayout ll = (LinearLayout) findViewById(R.layout.activity_main); 
    应该是寻找布局吧?因为 ll 类型是Linearlayout吧。
    R.id.activity_main 与 R.layout.activity_main,应该使用layout?
      

  8.   

    public View findViewById (int id)
    该函数利用我们在XML文件中定义的View的id属性来获取相应的View对象。
      

  9.   

    你在findViewById里写的类型不符
      

  10.   

    捎带又学了点儿东西,应该是你的程序的空指针的原因:
    在调用view中的findViewById()一定要想好父View是谁!即**view.findViewById()中的**view要找对,如果没有找对父View,返回基本都是null了
      

  11.   

    解决了!不过又有一个问题:
    我把这个设置放在一个 SetBackground 的类中: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);
    SetBackground()
    {
    // ll = (LinearLayout) findViewById(R.id.activity_main);
    }
    }然后在Main_Activity中onCreate里面new一个: SetBackground sbg = new SetBackground();
    Log.v("handle","new sbg up!");//结果又是空指针错误,这是为什么呢?
      

  12.   

    解决了!不过又有一个问题:
    我把这个设置放在一个 SetBackground 的类中: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);
    SetBackground()
    {
    // ll = (LinearLayout) findViewById(R.id.activity_main);
    }
    }然后在Main_Activity中onCreate里面new一个: SetBackground sbg = new SetBackground();
    Log.v("handle","new sbg up!");//结果又是空指针错误,这是为什么呢?
    空指针会指明哪一行,具体是哪个?
      

  13.   

    解决了!不过又有一个问题:
    我把这个设置放在一个 SetBackground 的类中: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);
    SetBackground()
    {
    // ll = (LinearLayout) findViewById(R.id.activity_main);
    }
    }然后在Main_Activity中onCreate里面new一个: SetBackground sbg = new SetBackground();
    Log.v("handle","new sbg up!");//结果又是空指针错误,这是为什么呢?
    空指针会指明哪一行,具体是哪个?
    我一开始的问题是MainActivity.java的类
    里面直接修改背景,经过大家的帮助解决问题了,因此我想把修改背景的功能独立出来,放在另外一个SetBackground.java的类中,通过调用SetBackground.java来的方法修改背景:
    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);
    }
    }
      

  14.   

    解决了!不过又有一个问题:
    我把这个设置放在一个 SetBackground 的类中: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);
    SetBackground()
    {
    // ll = (LinearLayout) findViewById(R.id.activity_main);
    }
    }然后在Main_Activity中onCreate里面new一个: SetBackground sbg = new SetBackground();
    Log.v("handle","new sbg up!");//结果又是空指针错误,这是为什么呢?
    空指针会指明哪一行,具体是哪个?
    我一开始的问题是MainActivity.java的类
    里面直接修改背景,经过大家的帮助解决问题了,因此我想把修改背景的功能独立出来,放在另外一个SetBackground.java的类中,通过调用SetBackground.java来的方法修改背景:
    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);
    }
    }
    你怎么能new一个activity……都继承了activity你怎么new它
      

  15.   

    空指针会指明哪一行,具体是哪个?
    我一开始的问题是MainActivity.java的类
    里面直接修改背景,经过大家的帮助解决问题了,因此我想把修改背景的功能独立出来,放在另外一个SetBackground.java的类中,通过调用SetBackground.java来的方法修改背景:
    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);
    }
    }
    有问题。。你这个是继承一个activity,你不能在oncreate之前进行
     findViewById或者使用资源文件。会找不到的。
      

  16.   

    空指针会指明哪一行,具体是哪个?
    我一开始的问题是MainActivity.java的类
    里面直接修改背景,经过大家的帮助解决问题了,因此我想把修改背景的功能独立出来,放在另外一个SetBackground.java的类中,通过调用SetBackground.java来的方法修改背景:
    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);
    }
    }
    有问题。。你这个是继承一个activity,你不能在oncreate之前进行
     findViewById或者使用资源文件。会找不到的。额,这怎么办我想试图将程序模块化,以后我想把 设置背景、设置音量都放在一个专门的Setting.java中,现在连设置背景都不能单独搞一个类,以后主函数可真冗长的……
    另,我的setBackground.java必须extends acticity,要不然
    Resources resources = getBaseContext().getResources();
    Drawable btnDrawable = resources.getDrawable(R.drawable.green);;
    这两行会出错,错误在于,找不到getBaseContext()。而继承Activity,就有了……木有办法呀……

      

  17.   

    有个想法,你的更换背景不要放在oncreat,还有其他生命周期。
    主要是你要等onCreat里面把控件加载后,才能找到id
      

  18.   


    你可以传context例如:public static Drawable setMyDrawable(Context context){
    Resources resources = context.getResources();
    Drawable btnDrawable =resources.getDrawable(R.drawable.ic_launcher);
    return btnDrawable;
    }这样你就获得了一个drawable
    这个是临时写的,你可以考虑下完善它。
      

  19.   


    你可以传context例如:public static Drawable setMyDrawable(Context context){
    Resources resources = context.getResources();
    Drawable btnDrawable =resources.getDrawable(R.drawable.ic_launcher);
    return btnDrawable;
    }这样你就获得了一个drawable
    这个是临时写的,你可以考虑下完善它。
    Contest我刚查了一些资料,但是觉得没有实例难以理解,在我这个问题中——限于水平——你给出的这个静态方法,是放在SetBackground.java类中吗?具体如何调用?但我觉得,问题不是在这儿吧,系统提示出错是 空指针,但我没觉得哪儿数据传递有问题呀?
      

  20.   

    我在网上查了一些资料,似乎说Context的比较多,接口什么的没有相关信息……
    另外,我觉得这个问题应该很普遍吧,怎么没有相关信息呢?
    MainActivity.java开始时提取Options.java的内容,更换自己背景
    Options.java设置MainActivity的背景
    发现MainActivity类的东西Options一用,就空指针错误……
      

  21.   

    我在网上查了一些资料,似乎说Context的比较多,接口什么的没有相关信息……
    另外,我觉得这个问题应该很普遍吧,怎么没有相关信息呢?
    MainActivity.java开始时提取Options.java的内容,更换自己背景
    Options.java设置MainActivity的背景
    发现MainActivity类的东西Options一用,就空指针错误……
    上面说的是theme。用setTheme可以更改主题,在manifest文件里的
    <application android:theme="@style/AppTheme" >就是一种。像android:Theme.Light~嗯,刚才的setMyDrawable是放在其他类里的,当成工具类使用里面的R.drawable.ic_launcher可以当成一个参数传入
    这样就把获取drawable的工作抽离开来了。
    mvc思想空指针问题我已经说过了,oncreate之前获取不了资源文件resources是空的……
      

  22.   

    Contest我刚查了一些资料,但是觉得没有实例难以理解,在我这个问题中——限于水平——你给出的这个静态方法,是放在SetBackground.java类中吗?具体如何调用?但我觉得,问题不是在这儿吧,系统提示出错是 空指针,但我没觉得哪儿数据传递有问题呀?
    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,因为都没初始化。这就是你那空指针的问题
      

  23.   

    Contest我刚查了一些资料,但是觉得没有实例难以理解,在我这个问题中——限于水平——你给出的这个静态方法,是放在SetBackground.java类中吗?具体如何调用?但我觉得,问题不是在这儿吧,系统提示出错是 空指针,但我没觉得哪儿数据传递有问题呀?
    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,因为都没初始化。这就是你那空指针的问题
    呃,前辈,我的resources赋值是在onCreate之后,只不过是在MainActivity的界面中,调取SetBackground界面中的方法造成了空指针问题,如果直接在MainActivity中自己调用,就没有这个错误……
      

  24.   

    不懂你说什么。。SetBackground的代码不是你12楼贴了么,
    “调取SetBackground界面中的方法造成了空指针问题,”这句话什么意思,你SetBackground哪里有方法可以调用。。就一个构造函数。。
    还是说SetBackground界面中调用getBaseContext().getResources()?   那这个错误在24楼已经验证了……
      

  25.   

    不懂你说什么。。SetBackground的代码不是你12楼贴了么,
    “调取SetBackground界面中的方法造成了空指针问题,”这句话什么意思,你SetBackground哪里有方法可以调用。。就一个构造函数。。
    还是说SetBackground界面中调用getBaseContext().getResources()?   那这个错误在24楼已经验证了……
    我把SetBackground这个类删除了,而把这个类实现的方法,放在Options.java里面去了,是在onCreate之后调运的,出现了空指针错误~
      

  26.   

    贴代码,要看你怎么用的。。不过为啥optins里有oncreate?
    有oncreate就要创建activity,但是你有是把这个activity当成一个工具调用……功能要分开
    ————
    错误猜测:
    activity没创建,oncreate都没调用……所以根本没初始化导致空指针。
    如果是这样,你对activity的知识需要恶补一下……
      

  27.   

    贴代码,要看你怎么用的。。不过为啥optins里有oncreate?
    有oncreate就要创建activity,但是你有是把这个activity当成一个工具调用……功能要分开
    ————
    错误猜测:
    activity没创建,oncreate都没调用……所以根本没初始化导致空指针。
    如果是这样,你对activity的知识需要恶补一下……
    有道理,MainActivity是一个界面(主界面),Options.java是一个界面(设置界面)
    在MainActivity调用Options方法的时候,设置界面当然没有启动,估计就是这个的原因吧,但是单独创建一个类,没有onCreate也是同样的错误——看来我真要恶补activity了,可惜官网被屏蔽了……
      

  28.   

    可以找找入门视频看,一些博客写的也不错。
    自己也要写写demo跑一下,生命周期什么的,写一下看下输出结果就明白了。不用activity可以的,只要有context上下文,就可以调用资源文件了。 
     或者用theme把,不过切换主题要刷新下界面,数据会没的。不过可以刷新保存数据再加载新的theme
      

  29.   

    可以找找入门视频看,一些博客写的也不错。
    自己也要写写demo跑一下,生命周期什么的,写一下看下输出结果就明白了。不用activity可以的,只要有context上下文,就可以调用资源文件了。 
     或者用theme把,不过切换主题要刷新下界面,数据会没的。不过可以刷新保存数据再加载新的theme
    呃,好吧……能推荐一些相关的教程资源吗?