尝试保存首选项(根据官方 android 参考)会导致错误
当用户跳转到应用程序的其他页面(活动)时,我需要保存主活动的一些变量。官方参考(http://developer.android.com/reference/android/app/Activity.html#SavingPercientState)提供了使用下一个代码来保存持久状态:
public class CalendarActivity extends Activity {
...
static final int DAY_VIEW_MODE = 0;
static final int WEEK_VIEW_MODE = 1;
private SharedPreferences mPrefs;
private int mCurViewMode;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences mPrefs = getSharedPreferences();
mCurViewMode = mPrefs.getInt("view_mode" DAY_VIEW_MODE);
}
protected void onPause() {
super.onPause();
SharedPreferences.Editor ed = mPrefs.edit();
ed.putInt("view_mode", mCurViewMode);
ed.commit();
}
}
当我将这部分代码实现到我的应用程序时(有点区别,而不是 ed.putInt 我使用 ed.putBoolean) 并运行它,我在 LOGCat 中收到错误。
10-21 15:00:42.956: 错误/AndroidRuntime(26590): 致命异常: main 10-21 15:00:42.956: 错误/AndroidRuntime(26590): java.lang.RuntimeException:无法暂停活动 {com.example.android.Pitbul/com.example.android.Soft.Commander}: java.lang.NullPointerException 10-21 15:00:42.956: 错误/AndroidRuntime(26590):位于 android.app.ActivityThread.performPauseActivity(ActivityThread.java:2731) 10-21 15:00:42.956: 错误/AndroidRuntime(26590): 在 android.app.ActivityThread.performPauseActivity(ActivityThread.java:2678) 10-21 15:00:42.956: 错误/AndroidRuntime(26590): 在 android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3259) 10-21 15:00:42.956: 错误/AndroidRuntime(26590): 在 android.app.ActivityThread.access$1600(ActivityThread.java:132) 10-21 15:00:42.956: 错误/AndroidRuntime(26590): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1042) ... 10-21 15:00:42.956:错误/AndroidRuntime(26590):引起的: java.lang.NullPointerException 10-21 15:00:42.956: 错误/AndroidRuntime(26590):位于 com.example.android.Soft.Commander.onPause(Commander.java:355) 10-21 15:00:42.956: 错误/AndroidRuntime(26590): 在 android.app.Activity.performPause(Activity.java:4032) 10-21 15:00:42.956: 错误/AndroidRuntime(26590): 在 android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1337) 10-21 15:00:42.956: 错误/AndroidRuntime(26590): 在 android.app.ActivityThread.performPauseActivity(ActivityThread.java:2708) 10-21 15:00:42.956: 错误/AndroidRuntime(26590): ... 12 更多
因此,错误发生在 SharedPreferences.Editor ed = mPrefs.edit();细绳。
为什么会发生这样的事?我需要什么来解决这个问题?我确实需要保存一些变量并在用户返回主活动屏幕时读取它们。
I need to save some variables of my main Activity when the user jumping to another pages (activities) of my application. The official reference (http://developer.android.com/reference/android/app/Activity.html#SavingPersistentState) is provide to do saving persistent state by using the next code:
public class CalendarActivity extends Activity {
...
static final int DAY_VIEW_MODE = 0;
static final int WEEK_VIEW_MODE = 1;
private SharedPreferences mPrefs;
private int mCurViewMode;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences mPrefs = getSharedPreferences();
mCurViewMode = mPrefs.getInt("view_mode" DAY_VIEW_MODE);
}
protected void onPause() {
super.onPause();
SharedPreferences.Editor ed = mPrefs.edit();
ed.putInt("view_mode", mCurViewMode);
ed.commit();
}
}
When I implemented this part of code to my application (a little difference instead of ed.putInt I use ed.putBoolean) and run it, I got an error in LOGCat.
10-21 15:00:42.956: ERROR/AndroidRuntime(26590): FATAL EXCEPTION: main
10-21 15:00:42.956: ERROR/AndroidRuntime(26590):
java.lang.RuntimeException: Unable to pause activity
{com.example.android.Pitbul/com.example.android.Soft.Commander}:
java.lang.NullPointerException 10-21 15:00:42.956:
ERROR/AndroidRuntime(26590): at
android.app.ActivityThread.performPauseActivity(ActivityThread.java:2731)
10-21 15:00:42.956: ERROR/AndroidRuntime(26590): at
android.app.ActivityThread.performPauseActivity(ActivityThread.java:2678)
10-21 15:00:42.956: ERROR/AndroidRuntime(26590): at
android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3259)
10-21 15:00:42.956: ERROR/AndroidRuntime(26590): at
android.app.ActivityThread.access$1600(ActivityThread.java:132) 10-21
15:00:42.956: ERROR/AndroidRuntime(26590): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1042)
... 10-21 15:00:42.956: ERROR/AndroidRuntime(26590): Caused by:
java.lang.NullPointerException 10-21 15:00:42.956:
ERROR/AndroidRuntime(26590): at
com.example.android.Soft.Commander.onPause(Commander.java:355) 10-21
15:00:42.956: ERROR/AndroidRuntime(26590): at
android.app.Activity.performPause(Activity.java:4032) 10-21
15:00:42.956: ERROR/AndroidRuntime(26590): at
android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1337)
10-21 15:00:42.956: ERROR/AndroidRuntime(26590): at
android.app.ActivityThread.performPauseActivity(ActivityThread.java:2708)
10-21 15:00:42.956: ERROR/AndroidRuntime(26590): ... 12 more
So, the error is happen on the
SharedPreferences.Editor ed = mPrefs.edit(); string.
Why this happened? What I need to fix this problem? I really need to save some variables and read them when user back on main activity screen.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这样做:
您在这一行中做错了:
在
onCreate()
中将其设为局部变量,因此无法初始化全局变量,从而导致NullPointerException
onPause()
do it like this way:
You are doing wrong in this line:
making it local variable in
onCreate()
, so the global variable couldn't be initialized which causeNullPointerException
inonPause()