Tmmccon Tmmccon - 1 month ago 17
Android Question

SharedPreferences.Editor NPE

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?)

10-08 08:28:11.740 23625-23625/? D/dalvikvm: Late-enabling CheckJNI
10-08 08:28:11.985 23625-23625/temperatureconverter.android.vogella.com.prefsfromandroiddev I/dalvikvm: Could not find method android.app.Application.registerOnProvideAssistDataListener, referenced from method com.android.tools.fd.runtime.BootstrapApplication.registerOnProvideAssistDataListener
10-08 08:28:11.985 23625-23625/temperatureconverter.android.vogella.com.prefsfromandroiddev W/dalvikvm: VFY: unable to resolve virtual method 223: Landroid/app/Application;.registerOnProvideAssistDataListener (Landroid/app/Application$OnProvideAssistDataListener;)V
10-08 08:28:11.985 23625-23625/temperatureconverter.android.vogella.com.prefsfromandroiddev D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
10-08 08:28:11.985 23625-23625/temperatureconverter.android.vogella.com.prefsfromandroiddev I/dalvikvm: Could not find method android.app.Application.unregisterOnProvideAssistDataListener, referenced from method com.android.tools.fd.runtime.BootstrapApplication.unregisterOnProvideAssistDataListener
10-08 08:28:11.985 23625-23625/temperatureconverter.android.vogella.com.prefsfromandroiddev W/dalvikvm: VFY: unable to resolve virtual method 226: Landroid/app/Application;.unregisterOnProvideAssistDataListener (Landroid/app/Application$OnProvideAssistDataListener;)V
10-08 08:28:11.985 23625-23625/temperatureconverter.android.vogella.com.prefsfromandroiddev D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
10-08 08:28:11.985 23625-23625/temperatureconverter.android.vogella.com.prefsfromandroiddev I/InstantRun: Instant Run Runtime started. Android package is temperatureconverter.android.vogella.com.prefsfromandroiddev, real application class is null.
10-08 08:28:11.990 23625-23625/temperatureconverter.android.vogella.com.prefsfromandroiddev W/InstantRun: No instant run dex files added to classpath
10-08 08:28:12.010 23625-23625/temperatureconverter.android.vogella.com.prefsfromandroiddev E/dalvikvm: Could not find class 'android.util.ArrayMap', referenced from method com.android.tools.fd.runtime.MonkeyPatcher.monkeyPatchExistingResources
10-08 08:28:12.010 23625-23625/temperatureconverter.android.vogella.com.prefsfromandroiddev W/dalvikvm: VFY: unable to resolve check-cast 1783 (Landroid/util/ArrayMap;) in Lcom/android/tools/fd/runtime/MonkeyPatcher;
10-08 08:28:12.010 23625-23625/temperatureconverter.android.vogella.com.prefsfromandroiddev D/dalvikvm: VFY: replacing opcode 0x1f at 0x025e
10-08 08:28:12.015 23625-23625/temperatureconverter.android.vogella.com.prefsfromandroiddev E/dalvikvm: Could not find class 'android.util.ArrayMap', referenced from method com.android.tools.fd.runtime.MonkeyPatcher.pruneResourceCache
10-08 08:28:12.015 23625-23625/temperatureconverter.android.vogella.com.prefsfromandroiddev W/dalvikvm: VFY: unable to resolve const-class 1783 (Landroid/util/ArrayMap;) in Lcom/android/tools/fd/runtime/MonkeyPatcher;
10-08 08:28:12.015 23625-23625/temperatureconverter.android.vogella.com.prefsfromandroiddev D/dalvikvm: VFY: replacing opcode 0x1c at 0x0060
10-08 08:28:12.065 23625-23625/temperatureconverter.android.vogella.com.prefsfromandroiddev W/dalvikvm: VFY: unable to find class referenced in signature (Landroid/view/SearchEvent;)
10-08 08:28:12.065 23625-23625/temperatureconverter.android.vogella.com.prefsfromandroiddev I/dalvikvm: Could not find method android.view.Window$Callback.onSearchRequested, referenced from method android.support.v7.view.WindowCallbackWrapper.onSearchRequested
10-08 08:28:12.065 23625-23625/temperatureconverter.android.vogella.com.prefsfromandroiddev W/dalvikvm: VFY: unable to resolve interface method 15400: Landroid/view/Window$Callback;.onSearchRequested (Landroid/view/SearchEvent;)Z
10-08 08:28:12.065 23625-23625/temperatureconverter.android.vogella.com.prefsfromandroiddev D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
10-08 08:28:12.065 23625-23625/temperatureconverter.android.vogella.com.prefsfromandroiddev I/dalvikvm: Could not find method android.view.Window$Callback.onWindowStartingActionMode, referenced from method android.support.v7.view.WindowCallbackWrapper.onWindowStartingActionMode
10-08 08:28:12.065 23625-23625/temperatureconverter.android.vogella.com.prefsfromandroiddev W/dalvikvm: VFY: unable to resolve interface method 15404: Landroid/view/Window$Callback;.onWindowStartingActionMode (Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode;
10-08 08:28:12.065 23625-23625/temperatureconverter.android.vogella.com.prefsfromandroiddev D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
10-08 08:28:12.070 23625-23625/temperatureconverter.android.vogella.com.prefsfromandroiddev I/System.out: onCreate()
10-08 08:28:12.135 23625-23625/temperatureconverter.android.vogella.com.prefsfromandroiddev I/dalvikvm: Could not find method android.view.ViewGroup.onRtlPropertiesChanged, referenced from method android.support.v7.widget.Toolbar.onRtlPropertiesChanged
10-08 08:28:12.135 23625-23625/temperatureconverter.android.vogella.com.prefsfromandroiddev W/dalvikvm: VFY: unable to resolve virtual method 15299: Landroid/view/ViewGroup;.onRtlPropertiesChanged (I)V
10-08 08:28:12.135 23625-23625/temperatureconverter.android.vogella.com.prefsfromandroiddev D/dalvikvm: VFY: replacing opcode 0x6f at 0x0007
10-08 08:28:12.140 23625-23625/temperatureconverter.android.vogella.com.prefsfromandroiddev I/dalvikvm: Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.widget.TintTypedArray.getChangingConfigurations
10-08 08:28:12.140 23625-23625/temperatureconverter.android.vogella.com.prefsfromandroiddev W/dalvikvm: VFY: unable to resolve virtual method 644: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
10-08 08:28:12.140 23625-23625/temperatureconverter.android.vogella.com.prefsfromandroiddev D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
10-08 08:28:12.140 23625-23625/temperatureconverter.android.vogella.com.prefsfromandroiddev I/dalvikvm: Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.widget.TintTypedArray.getType
10-08 08:28:12.140 23625-23625/temperatureconverter.android.vogella.com.prefsfromandroiddev W/dalvikvm: VFY: unable to resolve virtual method 666: Landroid/content/res/TypedArray;.getType (I)I
10-08 08:28:12.140 23625-23625/temperatureconverter.android.vogella.com.prefsfromandroiddev D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
10-08 08:28:12.220 23625-23630/temperatureconverter.android.vogella.com.prefsfromandroiddev D/dalvikvm: GC_CONCURRENT freed 262K, 12% free 7213K/8135K, paused 15ms+16ms, total 53ms
10-08 08:28:12.245 23625-23625/temperatureconverter.android.vogella.com.prefsfromandroiddev D/libEGL: loaded /system/lib/egl/libEGL_mali.so
10-08 08:28:12.245 23625-23625/temperatureconverter.android.vogella.com.prefsfromandroiddev D/libEGL: loaded /system/lib/egl/libGLESv1_CM_mali.so
10-08 08:28:12.250 23625-23625/temperatureconverter.android.vogella.com.prefsfromandroiddev D/libEGL: loaded /system/lib/egl/libGLESv2_mali.so

[ 10-08 08:28:12.250 23625:23625 D/ ]
Device driver API match
Device driver API version: 10
User space API version: 10


[ 10-08 08:28:12.250 23625:23625 D/ ]
mali: REVISION=Linux-r2p4-02rel0 BUILD_DATE=Tue Oct 16 15:37:13 KST 2012


10-08 08:28:12.280 23625-23625/temperatureconverter.android.vogella.com.prefsfromandroiddev D/OpenGLRenderer: Enabling debug mode 0
10-08 08:28:12.285 23625-23625/temperatureconverter.android.vogella.com.prefsfromandroiddev I/dalvikvm: Could not find method android.content.res.Resources.getDrawable, referenced from method android.support.v7.widget.ResourcesWrapper.getDrawable
10-08 08:28:12.285 23625-23625/temperatureconverter.android.vogella.com.prefsfromandroiddev W/dalvikvm: VFY: unable to resolve virtual method 607: Landroid/content/res/Resources;.getDrawable (ILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
10-08 08:28:12.285 23625-23625/temperatureconverter.android.vogella.com.prefsfromandroiddev D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
10-08 08:28:12.285 23625-23625/temperatureconverter.android.vogella.com.prefsfromandroiddev I/dalvikvm: Could not find method android.content.res.Resources.getDrawableForDensity, referenced from method android.support.v7.widget.ResourcesWrapper.getDrawableForDensity
10-08 08:28:12.285 23625-23625/temperatureconverter.android.vogella.com.prefsfromandroiddev W/dalvikvm: VFY: unable to resolve virtual method 609: Landroid/content/res/Resources;.getDrawableForDensity (IILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
10-08 08:28:12.285 23625-23625/temperatureconverter.android.vogella.com.prefsfromandroiddev D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
10-08 08:28:17.305 23625-23625/temperatureconverter.android.vogella.com.prefsfromandroiddev I/System.out: onPause()
10-08 08:28:17.305 23625-23625/temperatureconverter.android.vogella.com.prefsfromandroiddev D/AndroidRuntime: Shutting down VM
10-08 08:28:17.305 23625-23625/temperatureconverter.android.vogella.com.prefsfromandroiddev W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x41bd02a0)
10-08 08:28:17.310 23625-23625/temperatureconverter.android.vogella.com.prefsfromandroiddev E/AndroidRuntime: FATAL EXCEPTION: main
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) 
10-08 08:28:27.795 23625-23625/temperatureconverter.android.vogella.com.prefsfromandroiddev I/Process: Sending signal. PID: 23625 SIG: 9

Answer

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