I have 2 Activities, SignIn and ChooseLanguage. On choosing a particular language, the language chosen is saved via shared preferences and the application goes to the SignIn activity via Intent. All this is working perfectly fine. There is a loadLocale() method in the ChooseLanguage class which i want to call from the SignIn class. This however gives a null pointer exception. NOTE: The language preference is saved correctly, i have opened it and seen it myself. Where am i going wrong?
In ChooseLanguage Activity:
public void loadLocale(){
Log.i("TAG","CAME HERE");
SharedPreferences prefs = getSharedPreferences("LanguageSettings", Activity.MODE_PRIVATE);
String preferredLanguage = prefs.getString("PreferredLang","");
setLocale(preferredLanguage);
}
In SignIn Activity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ChooseLanguage chooseLanguage = new ChooseLanguage();
chooseLanguage.loadLocale();
setContentView(R.layout.sign_in);
.
.
.
}
My Log
2019-06-13 11:58:35.302 10533-10533/com.example.gofresh I/TAG: CAME HERE
2019-06-13 11:58:35.304 10533-10533/com.example.gofresh E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.gofresh, PID: 10533
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gofresh/com.example.gofresh.SignIn}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.SharedPreferences android.content.Context.getSharedPreferences(java.lang.String, int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3037)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3172)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1906)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6863)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.SharedPreferences android.content.Context.getSharedPreferences(java.lang.String, int)' on a null object reference
at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:174)
at com.example.gofresh.ChooseLanguage.loadLocale(ChooseLanguage.java:103)
at com.example.gofresh.SignIn.onCreate(SignIn.java:64)
at android.app.Activity.performCreate(Activity.java:7149)
at android.app.Activity.performCreate(Activity.java:7140)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1288)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3017)
The setLocale() method added here
private void setLocale(String preferredLanguage){
Locale locale = new Locale(preferredLanguage);
Locale.setDefault(locale);
Configuration configuration = new Configuration();
configuration.locale = locale;
getBaseContext().getResources().updateConfiguration(configuration, getBaseContext().getResources().getDisplayMetrics());
if(!preferredLanguage.equals(""))
onConfigurationChanged(configuration);
SharedPreferences.Editor editor = getSharedPreferences("LanguageSettings",MODE_PRIVATE).edit();
editor.putString("PreferredLang",preferredLanguage);
editor.apply();
}
@Override
public void onConfigurationChanged(Configuration newConfig)
{
chooseLanguage.setText(R.string.chooseYourLanguage);
okButton.setText(R.string.ok);
super.onConfigurationChanged(newConfig);
}