我一直在与共享的首选项做斗争,我看过帖子和教程,但无法让它发挥作用。
我最终从android.dev复制了这段代码,它对我也不起作用。我唯一的改变就是
它在onPause()中的SharedPreferences.Editor中使用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)发布于 2016-10-08 13:23:54
您的代码将在mPrefs函数中创建和初始化onCreate函数,因此它与oncreate外部的private SharedPreferences mPrefs;不一样,因此mPrefs;的作用域仅限于oncreate,不能在外部使用(将为null)。
因此,要使它对当前类全局化,请使用全局引用变量,不要在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();
}
}发布于 2016-10-08 13:26:35
在onCreate(Bundle savedInstanceState)中使用这个
mPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());而不是写作
SharedPreferences mPrefs = getSharedPreferences("MyPrefFile", 0);https://stackoverflow.com/questions/39932787
复制相似问题