1

I have been struggling with shared preferences and looked at posts and tutorials and can't get it to work.

I finally copied this code from android.dev and it won't work for me either. The only change I made was
1. extends AppCompatActivity rather than Activity.
2. add the printlns and
3. the parms to getSharedPreferences.

It fails with NPE in onPause() on the SharedPreferences.Editor. I don't find any exceptions in onStart().

Can someone please tell me what I am missing? Thank you in advance!

public class MainActivity extends AppCompatActivity {
    static final int DAY_VIEW_MODE = 0;
    static final int WEEK_VIEW_MODE = 1;

    private SharedPreferences mPrefs;
    private int mCurViewMode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        System.out.println("onCreate() ");
        SharedPreferences mPrefs = getSharedPreferences("MyPrefFile", 0);
        mCurViewMode = mPrefs.getInt("view_mode", DAY_VIEW_MODE);
    }

    protected void onPause() {
        super.onPause();

        System.out.println("onPause() ");
        SharedPreferences.Editor ed = mPrefs.edit();
        ed.putInt("view_mode", mCurViewMode);
        ed.commit();
    }
}

Here is the logcat, is that the error log you requested? Sorry, I am pretty new to this. (ug... is there a better way to share this?)

java.lang.RuntimeException: Unable to pause activity {temperatureconverter.android.vogella.com.prefsfromandroiddev/temperatureconverter.android.vogella.com.prefsfromandroiddev.MainActivity}: java.lang.NullPointerException
  at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2902)
  at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2858)
  at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:2836)
  at android.app.ActivityThread.access$900(ActivityThread.java:140)
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1254)
  at android.os.Handler.dispatchMessage(Handler.java:99)
  at android.os.Looper.loop(Looper.java:137)
  at android.app.ActivityThread.main(ActivityThread.java:4921)
  at java.lang.reflect.Method.invokeNative(Native Method)
  at java.lang.reflect.Method.invoke(Method.java:511)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
  at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
  at temperatureconverter.android.vogella.com.prefsfromandroiddev.MainActivity.onPause(MainActivity.java:38)
  at android.app.Activity.performPause(Activity.java:5286)
  at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1240)
  at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2889)
  at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2858) 
  at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:2836) 
  at android.app.ActivityThread.access$900(ActivityThread.java:140) 
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1254) 
  at android.os.Handler.dispatchMessage(Handler.java:99) 
  at android.os.Looper.loop(Looper.java:137) 
  at android.app.ActivityThread.main(ActivityThread.java:4921) 
  at java.lang.reflect.Method.invokeNative(Native Method) 
  at java.lang.reflect.Method.invoke(Method.java:511) 
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038) 
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805) 
  at dalvik.system.NativeStart.main(Native Method)
hveiga
  • 6,725
  • 7
  • 54
  • 78
Tmmccon
  • 39
  • 4

2 Answers2

2

Your code will create and initialize the mPrefs inside onCreate function so it is not same as the one you have outside oncreate i.e private SharedPreferences mPrefs;, so scope of mPrefs; is limited to your oncreate and can't be used outside (will be null)

So to make it global to current class, use the global reference variable and don't declare it again inside your oncreate function so that it can be used inside onPause function, like this

 public class MainActivity extends AppCompatActivity {

    static final int DAY_VIEW_MODE = 0;
    static final int WEEK_VIEW_MODE = 1;

    private SharedPreferences mPrefs;
    private int mCurViewMode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        System.out.println("onCreate() ");
       // you are creating an another mPrefs reference local to oncreate function
       //  which is different from the global one ,so remove this 
       // SharedPreferences mPrefs = getSharedPreferences("MyPrefFile", 0);
        mPrefs = getSharedPreferences("MyPrefFile", 0); // use this
        mCurViewMode = mPrefs.getInt("view_mode", DAY_VIEW_MODE);
    }

    protected void onPause() {
        super.onPause();

        System.out.println("onPause() ");
        // you can do the same for editor to make it accessible to all functions 
        SharedPreferences.Editor ed = mPrefs.edit();
        ed.putInt("view_mode", mCurViewMode);
        ed.commit();
    }
}
Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68
-1

use this inside onCreate(Bundle savedInstanceState)

mPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

instead of writing

SharedPreferences mPrefs = getSharedPreferences("MyPrefFile", 0);
gautam90
  • 58
  • 7
  • @PavneetSingh answer is better because it explains what's happening and how both lines differ. Consider doing so in your future answers as well. Thanks. – Eugen Pechanec Oct 08 '16 at 18:28