0

I'm using shared preferences in my app to set different background colors.

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);
}

This code works fine, the color of mainlayout changes. But when I want change the color of simply row item

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);
}

In the xml file of this relative layout the id is 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"...
/>

Logs below:

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) 
Bartosz Czyżowski
  • 119
  • 1
  • 4
  • 13

3 Answers3

0

Set it as global variable in your activity before onCreate

private LinearLayout mainLayout;

and then in onCreate of your activity

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

and then in your method

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);
}

Hope it helps

Atiq
  • 14,435
  • 6
  • 54
  • 69
0

Your problem is that you're trying to find a view that is not under your current layout view structure. The RelativeLayout is the problem here it can't be found under the view you set in setContentView();.

Ahmed Hegazy
  • 12,395
  • 5
  • 41
  • 64
0

I understand from your question that you are using this RelativeLayout for an item of custom listview. And the View of your MainActivity is mainActivityLayout.

If my guess is true, you can't access this RelativeLayout from inside the MainActivity because the RelativeLayout not part of the mainActivityLayout,

but you can access it from inside the getView() function of the Adapter of your custom listview.

hope it helps

Mahmoud Ibrahim
  • 1,057
  • 11
  • 20