使用SharedPreferences时活动崩溃

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用SharedPreferences时活动崩溃相关的知识,希望对你有一定的参考价值。

public class FirstLevel extends Activity
{
     public final static String SCORE = "SCORE";
     public final static String DANE = "DANE";
     public final static String PASSED = "PASSED";

    boolean levelPassed;

    Bundle toLevelActivity;

    int id;

    TextView text;

    Intent fail, levelActivity;

    long t1, t2, tc;
    double score;

    SharedPreferences sharedPref;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.first_level);

        id=0;

        restoreData();

        text = (TextView)findViewById(R.id.text1);
        text.setText(id + "/10");

        fail = new Intent(this, FailActivity.class);
        levelActivity = new Intent(this, LevelsActivity.class);

        t1 = System.currentTimeMillis();

        toLevelActivity = new Bundle();


        if(levelPassed==true)
        {
            text.setText("Passed");
        }
        else if(levelPassed==false)
        {
            text.setText("NotPassed");
        }

    }
    private void saveData()
    {

        sharedPref = getSharedPreferences(DANE, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putBoolean(PASSED, true);
        editor.commit();
    }
    private void restoreData()
    {
    levelPassed = sharedPref.getBoolean(PASSED,true);
    }

...

levelPassed = true;
id = id +1;
saveData();
text.setText("You win! Your score: "+score3);
levelActivity.putExtra(SCORE,score3);

这是我的应用程序的代码片段。我有一个问题,因为Activity崩溃了。这段代码怎么样?

如果我删除这一行

levelPassed = sharedPref.getBoolean(PASSED,true);

一切都很好,但我需要这一行。

logcat的

 05-18 16:53:22.309: D/libEGL(2129): loaded /system/lib/egl/libGLES_android.so
05-18 16:53:22.319: D/libEGL(2129): loaded /system/lib/egl/libEGL_adreno200.so
05-18 16:53:22.349: D/libEGL(2129): loaded /system/lib/egl/libGLESv1_CM_adreno200.so
05-18 16:53:22.349: D/libEGL(2129): loaded /system/lib/egl/libGLESv2_adreno200.so
05-18 16:53:22.449: D/OpenGLRenderer(2129): Enabling debug mode 0
05-18 16:53:34.469: D/OpenGLRenderer(2129): Flushing caches (mode 0)
05-18 16:53:35.639: D/AndroidRuntime(2129): Shutting down VM
05-18 16:53:35.639: W/dalvikvm(2129): threadid=1: thread exiting with uncaught exception (group=0x40a4a1f8)
05-18 16:53:35.639: E/AndroidRuntime(2129): FATAL EXCEPTION: main
05-18 16:53:35.639: E/AndroidRuntime(2129): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mamory/com.example.mamory.FirstLevel}: java.lang.NullPointerException
05-18 16:53:35.639: E/AndroidRuntime(2129):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2079)
05-18 16:53:35.639: E/AndroidRuntime(2129):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2104)
05-18 16:53:35.639: E/AndroidRuntime(2129):     at android.app.ActivityThread.access$600(ActivityThread.java:132)
05-18 16:53:35.639: E/AndroidRuntime(2129):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1157)
05-18 16:53:35.639: E/AndroidRuntime(2129):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-18 16:53:35.639: E/AndroidRuntime(2129):     at android.os.Looper.loop(Looper.java:137)
05-18 16:53:35.639: E/AndroidRuntime(2129):     at android.app.ActivityThread.main(ActivityThread.java:4575)
05-18 16:53:35.639: E/AndroidRuntime(2129):     at java.lang.reflect.Method.invokeNative(Native Method)
05-18 16:53:35.639: E/AndroidRuntime(2129):     at java.lang.reflect.Method.invoke(Method.java:511)
05-18 16:53:35.639: E/AndroidRuntime(2129):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
05-18 16:53:35.639: E/AndroidRuntime(2129):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
05-18 16:53:35.639: E/AndroidRuntime(2129):     at dalvik.system.NativeStart.main(Native Method)
05-18 16:53:35.639: E/AndroidRuntime(2129): Caused by: java.lang.NullPointerException
05-18 16:53:35.639: E/AndroidRuntime(2129):     at com.example.mamory.FirstLevel.restoreData(FirstLevel.java:140)
05-18 16:53:35.639: E/AndroidRuntime(2129):     at com.example.mamory.FirstLevel.onCreate(FirstLevel.java:77)
05-18 16:53:35.639: E/AndroidRuntime(2129):     at android.app.Activity.performCreate(Activity.java:4465)
05-18 16:53:35.639: E/AndroidRuntime(2129):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
05-18 16:53:35.639: E/AndroidRuntime(2129):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2033)
05-18 16:53:35.639: E/AndroidRuntime(2129):     ... 11 more
答案

sharedPref为空:

05-18 16:53:35.639: E/AndroidRuntime(2129): Caused by: java.lang.NullPointerException
05-18 16:53:35.639: E/AndroidRuntime(2129):     at com.example.mamory.FirstLevel.restoreData(FirstLevel.java:140)

因为你在初始化restoreData()之前调用sharedPref。在restoreData()初始化它:

private void restoreData() {
    sharedPref = getSharedPreferences(DANE, Context.MODE_PRIVATE);
    levelPassed = sharedPref.getBoolean(PASSED,true);
}

或者更好地在onCreate()

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...
    sharedPref = getSharedPreferences(DANE, Context.MODE_PRIVATE);
    ...
}

如果在onCreate中初始化它,则无需在saveData()restoreData()中进行。

另一答案
case R.id.button1:    //this is to save
SharedPreferences.Editor editor=sha.edit();
editor.putBoolean("check", true);           
editor.commit();

case R.id.button2:    //this is to retrieve
boolean cht=sha.getBoolean("check", false);
if(cht){
t.setText("its working");
}
else{
t.setText("not working");
}
另一答案

移动这个

sharedPref = getSharedPreferences(DANE, Context.MODE_PRIVATE);

onCreate()

要么

复制

sharedPref = getSharedPreferences(DANE, Context.MODE_PRIVATE);

restoreData()

这将解决NPE

以上是关于使用SharedPreferences时活动崩溃的主要内容,如果未能解决你的问题,请参考以下文章

sharedpreferences android 内存 vmHeap

SharedPreferences无法在活动中使用

SharedPreferences 不保存布尔属性

什么会导致 iPad(第 3 代)在启动时崩溃但在模拟器中工作?

如何在改变sharedpreferences数据后自动改变android menuItem数据?

SharedPreferences会在使用前不断删除