SharedPreferences.Editor NPE

时间:2016-10-08 13:14:22

标签: java android

我一直在努力解决共享偏好问题并查看帖子和教程,但无法让它发挥作用。

我终于从android.dev复制了这段代码,它也不会为我工作。我做的唯一改变是 1.扩展AppCompatActivity而不是Activity 2.添加printlns和
3.参与getSharedPreferences。

在SharedPreferences.Editor上的onPause()中,NPE失败。我在onStart()中找不到任何例外。

有人可以告诉我我错过了什么吗?提前谢谢!

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

这是logcat,是您请求的错误日志吗?对不起,我对此很陌生。 (ug ...有更好的方法来分享这个吗?)

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)

2 个答案:

答案 0 :(得分:2)

您的代码将创建并初始化mPrefsonCreate函数,因此它与您oncreate之外的private SharedPreferences mPrefs;不同,即mPrefs;的范围,oncreate 1}}仅限于oncreate,不能在外面使用(将为空)

因此,要使其成为当前类的全局,请使用全局引用变量,并且不要在onPause函数内再次声明它,以便可以在 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(); } } 函数内使用它,就像这样

Range.ParagraphFormat.SpaceAfter = 0

答案 1 :(得分:-1)

在onCreate(Bundle savedInstanceState)

中使用它
mPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

而不是写

SharedPreferences mPrefs = getSharedPreferences("MyPrefFile", 0);