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)