0

我有包含两个阶段 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 更改为字符串来使用数组中的位置编号作为键。

4

1 回答 1

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);
于 2013-03-07T19:25:14.180 回答