使用存储阶段时SharedPreferences错误

时间:2013-03-07 19:04:44

标签: android sharedpreferences

我有一个包含两个阶段0或1的项目数组。它们都初始化为0.在我的一个活动中,我将某些项目的阶段0更改为1。我正在尝试保存这些阶段,并在我的应用程序再次启动时获取它们。

这是我改变阶段的活动:

public class LogoActivity extends Activity{

static SharedPreferences data;

protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_logo);
...
if(a==true){ 

      ImageAdapter.items[LogoSelectionActivity.pos] = 1;
      SharedPreferences.Editor ed = data.edit();
      ed.putInt(Integer.toString(LogoSelectionActivity.pos), 1);
      ed.commit();
   }
...
}

以下是从首选项中保存数据的活动:

public class LogoSelectionActivity extends Activity {

static int pos;

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    for(int i=0;i<5;i++){
        ImageAdapter.items[i] = LogoActivity.data.getInt(Integer.toString(i), 0);
    }
    setContentView(R.layout.activity_list_logos);
...
}

请注意,我写的第一项活动在第二项活动开始之前开始。 这是错误:

03-07 20:51:52.295: E/AndroidRuntime(25452): FATAL EXCEPTION: main
03-07 20:51:52.295: E/AndroidRuntime(25452): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.turkishlogoquiz/com.example.turkishlogoquiz.LogoSelectionActivity}: java.lang.NullPointerException
03-07 20:51:52.295: E/AndroidRuntime(25452):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
03-07 20:51:52.295: E/AndroidRuntime(25452):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
03-07 20:51:52.295: E/AndroidRuntime(25452):    at android.app.ActivityThread.access$600(ActivityThread.java:140)
03-07 20:51:52.295: E/AndroidRuntime(25452):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
03-07 20:51:52.295: E/AndroidRuntime(25452):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-07 20:51:52.295: E/AndroidRuntime(25452):    at android.os.Looper.loop(Looper.java:137)
03-07 20:51:52.295: E/AndroidRuntime(25452):    at android.app.ActivityThread.main(ActivityThread.java:4898)
03-07 20:51:52.295: E/AndroidRuntime(25452):    at java.lang.reflect.Method.invokeNative(Native Method)
03-07 20:51:52.295: E/AndroidRuntime(25452):    at java.lang.reflect.Method.invoke(Method.java:511)
03-07 20:51:52.295: E/AndroidRuntime(25452):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
03-07 20:51:52.295: E/AndroidRuntime(25452):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
03-07 20:51:52.295: E/AndroidRuntime(25452):    at dalvik.system.NativeStart.main(Native Method)
03-07 20:51:52.295: E/AndroidRuntime(25452): Caused by: java.lang.NullPointerException
03-07 20:51:52.295: E/AndroidRuntime(25452):    at com.example.turkishlogoquiz.LogoSelectionActivity.onCreate(LogoSelectionActivity.java:32)
03-07 20:51:52.295: E/AndroidRuntime(25452):    at android.app.Activity.performCreate(Activity.java:5206)
03-07 20:51:52.295: E/AndroidRuntime(25452):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
03-07 20:51:52.295: E/AndroidRuntime(25452):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)

我通过排除其他行来缩短代码。我希望可以理解。 请注意,我通过将int更改为string来使用数组中的位置编号。

1 个答案:

答案 0 :(得分:1)

保存

 SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", MODE_PRIVATE);
            SharedPreferences.Editor prefsEditor = myPrefs.edit();
            prefsEditor.putInt("x", 1);
            prefsEditor.commit();

阅读

SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", MODE_PRIVATE);
int d = myprefs.getInt("x", -1);