尝试保存首选项(根据官方 android 参考)会导致错误

发布于 2024-12-11 08:16:19 字数 2549 浏览 4 评论 0原文

当用户跳转到应用程序的其他页面(活动)时,我需要保存主活动的一些变量。官方参考(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天赋异禀 2024-12-18 08:16:19

这样做:

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);
//other setContentView() etc.
         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();
     }
 }

您在这一行中做错了:

SharedPreferences mPrefs = getSharedPreferences();

onCreate() 中将其设为局部变量,因此无法初始化全局变量,从而导致 NullPointerException onPause()

do it like this way:

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);
//other setContentView() etc.
         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();
     }
 }

You are doing wrong in this line:

SharedPreferences mPrefs = getSharedPreferences();

making it local variable in onCreate(), so the global variable couldn't be initialized which cause NullPointerException in onPause()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文