使用SharedPreferences时活动崩溃

时间:2014-05-18 16:41:59

标签: android nullpointerexception 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

3 个答案:

答案 0 :(得分:0)

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");
}

答案 1 :(得分:0)

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()中进行初始化。

答案 2 :(得分:0)

移动这个

sharedPref = getSharedPreferences(DANE, Context.MODE_PRIVATE);

onCreate()

OR

复制

sharedPref = getSharedPreferences(DANE, Context.MODE_PRIVATE);

restoreData()

这将解决NPE