以编程方式设置布局背景color = nullpointerexc

时间:2016-05-27 17:28:23

标签: android nullpointerexception

我在我的应用中使用共享偏好设置不同的背景颜色。

private void loadPreferences(){
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

    boolean isBackgroundDark = sharedPreferences.getBoolean("background_color",false);
    if(isBackgroundDark){
        LinearLayout mainLayout = (LinearLayout) findViewById(R.id.mainActivityLayout);
        mainLayout.setBackgroundColor(Color.parseColor("#3c3f41"));

    }
    String notebookTitle = sharedPreferences.getString("title","Notebook");
    setTitle(notebookTitle);
}

此代码工作正常,mainlayout的颜色会发生变化。 但是当我想要改变简单行项目的颜色时

private void loadPreferences(){         SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

    boolean isBackgroundDark = sharedPreferences.getBoolean("background_color",false);
    if(isBackgroundDark){
        LinearLayout mainLayout = (LinearLayout) findViewById(R.id.mainActivityLayout);
        mainLayout.setBackgroundColor(Color.parseColor("#3c3f41"));
        RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.list_row_rel_layout);
        relativeLayout.setBackgroundColor(Color.parseColor("#3c3f41"));
    }
    String notebookTitle = sharedPreferences.getString("title","Notebook");
    setTitle(notebookTitle);
}

在这个相对布局的xml文件中,id是define

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="#FFF9F9F9"
      android:id="@+id/list_row_rel_layout"
      android:paddingRight="10dp"
      android:paddingTop="10dp"
      android:paddingBottom="10dp"...
/>

以下日志:

FATAL EXCEPTION: main
                                                                     Process: com.bczyzowski.notebook, PID: 20754
                                                                     java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bczyzowski.notebook/com.bczyzowski.notebook.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RelativeLayout.setBackgroundColor(int)' on a null object reference
                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                         at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                         at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                         at android.os.Looper.loop(Looper.java:148)
                                                                         at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                         at java.lang.reflect.Method.invoke(Native Method)
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                      Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RelativeLayout.setBackgroundColor(int)' on a null object reference
                                                                         at com.bczyzowski.notebook.MainActivity.loadPreferences(MainActivity.java:77)
                                                                         at com.bczyzowski.notebook.MainActivity.onCreate(MainActivity.java:34)
                                                                         at android.app.Activity.performCreate(Activity.java:6237)
                                                                         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                         at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                         at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                         at android.os.Looper.loop(Looper.java:148) 
                                                                         at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                         at java.lang.reflect.Method.invoke(Native Method) 
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

3 个答案:

答案 0 :(得分:0)

onCreate

之前将其设置为活动中的全局变量
private LinearLayout mainLayout;

然后在您活动的onCreate

mainLayout = (LinearLayout) findViewById(R.id.mainActivityLayout);

然后在你的方法中

private void loadPreferences(){
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

    boolean isBackgroundDark = sharedPreferences.getBoolean("background_color",false);
        if(isBackgroundDark){
                    mainLayout.setBackgroundColor(Color.parseColor("#3c3f41"));
            }
            String notebookTitle = sharedPreferences.getString("title","Notebook");
            setTitle(notebookTitle);
}

希望有所帮助

答案 1 :(得分:0)

您的问题是您正在尝试查找不在当前布局视图结构下的视图。 RelativeLayout是问题,在setContentView();中设置的视图下无法找到。

答案 2 :(得分:0)

我从您的问题中了解到,您正在将 RelativeLayout 用于自定义列表视图 MainActivity 查看 mainActivityLayout

如果我的猜测属实,您无法从 MainActivity 中访问此 RelativeLayout ,因为 RelativeLayout 不属于 mainActivityLayout

但您可以从自定义列表视图的适配器 getView()功能中访问它。

希望有所帮助