Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot get shared preferences inside custom context wrapper injection

Tags:

java

android

I am trying to get a user's desired language stored in shared preferences, and use that to set the language of the app through this answer.

I have the same MyConxtextWrapper file as that answer but have changed the wrapper injection to:

    @Override
    protected void attachBaseContext(Context newBase) {
        SharedPreferences sharedPref = getSharedPreferences("userLang", Context.MODE_PRIVATE);
        String lang = sharedPref.getString("lang", "");


        super.attachBaseContext(MyContextWrapper.wrap(newBase,lang));
    }
}

This gives me the error:

04-01 18:35:25.648 25032-25032/com.example.c1629177.cymruniapp E/AndroidRuntime: FATAL EXCEPTION: main
 Process: com.example.c1629177.cymruniapp, PID: 25032
 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.c1629177.cymruniapp/com.example.c1629177.cymruniapp.MainActivity}: 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: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 'android.content.SharedPreferences android.content.Context.getSharedPreferences(java.lang.String, int)' on a null object reference
     at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:171)
     at com.example.c1629177.cymruniapp.MainActivity.attachBaseContext(MainActivity.java:194)
     at android.app.Activity.attach(Activity.java:6175)
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2350)
     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) 

How can I can pass the shared preference value "lang" into the line super.attachBaseContext(MyContextWrapper.wrap(newBase,lang)); ?

like image 567
ijames55 Avatar asked Apr 01 '17 17:04

ijames55


1 Answers

You're overriding attachBaseContext() which is used to attach the base context to your wrapper. At the moment when you're calling getSharedPreferences() the base context is not yet set and thus you're trying to get the NullPointerException.

So instead call this method directly on the newBase context:

@Override
protected void attachBaseContext(Context newBase) {
    SharedPreferences sharedPref = newBase.getSharedPreferences("userLang", Context.MODE_PRIVATE);
    String lang = sharedPref.getString("lang", "");
    super.attachBaseContext(MyContextWrapper.wrap(newBase, lang));
}

And maybe use a useful default for the language; instead of "".

like image 100
tynn Avatar answered Oct 21 '22 12:10

tynn