SharedPreference - 空指针异常

时间:2012-10-23 01:54:01

标签: android sharedpreferences

我正在尝试使用以下代码将内容保存到SharedPreferences:

public class SignupActivity extends Activity {

    String str;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.signup);

        str = PreferenceManager.getDefaultSharedPreferences(this).getString(
                "signedUp", null);

        SharedPreferences app_preferences = PreferenceManager
                .getDefaultSharedPreferences(this);

        String text = app_preferences.getString("signedUp", null);

        if(str.equals("YES")){
            startActivity(new Intent("com.mobi.job.scout.MyTabActivity"));
        }

        markAsSignedUp();

    }

    public void markAsSignedUp() {
        SharedPreferences.Editor localEditor = PreferenceManager
                .getDefaultSharedPreferences(getBaseContext()).edit();
        localEditor.putString("signedUp", "YES");
        localEditor.commit();
        Log.d("Signed Up!", ":)");
    }

}

我已阅读并关注了互联网上的一些教程,但我收到的错误如下。 这是logcat:

10-23 01:47:27.061: E/AndroidRuntime(408): FATAL EXCEPTION: main
10-23 01:47:27.061: E/AndroidRuntime(408): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mobi.job.scout/com.mobi.job.scout.SignupActivity}: java.lang.NullPointerException
10-23 01:47:27.061: E/AndroidRuntime(408):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
10-23 01:47:27.061: E/AndroidRuntime(408):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
10-23 01:47:27.061: E/AndroidRuntime(408):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
10-23 01:47:27.061: E/AndroidRuntime(408):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
10-23 01:47:27.061: E/AndroidRuntime(408):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-23 01:47:27.061: E/AndroidRuntime(408):  at android.os.Looper.loop(Looper.java:123)
10-23 01:47:27.061: E/AndroidRuntime(408):  at android.app.ActivityThread.main(ActivityThread.java:4627)
10-23 01:47:27.061: E/AndroidRuntime(408):  at java.lang.reflect.Method.invokeNative(Native Method)
10-23 01:47:27.061: E/AndroidRuntime(408):  at java.lang.reflect.Method.invoke(Method.java:521)
10-23 01:47:27.061: E/AndroidRuntime(408):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-23 01:47:27.061: E/AndroidRuntime(408):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-23 01:47:27.061: E/AndroidRuntime(408):  at dalvik.system.NativeStart.main(Native Method)
10-23 01:47:27.061: E/AndroidRuntime(408): Caused by: java.lang.NullPointerException
10-23 01:47:27.061: E/AndroidRuntime(408):  at com.mobi.job.scout.SignupActivity.onCreate(SignupActivity.java:28)
10-23 01:47:27.061: E/AndroidRuntime(408):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-23 01:47:27.061: E/AndroidRuntime(408):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
10-23 01:47:27.061: E/AndroidRuntime(408):  ... 11 more

请让我知道我正在使用的代码有什么问题,我总是最终强行关闭。感谢

1 个答案:

答案 0 :(得分:8)

str = PreferenceManager.getDefaultSharedPreferences(this).getString(
                "signedUp", null);

您的变量str为null,因为“signedUp”的值之前没有设置过值。在if语句中检查null。

if(str != null && str.equals("YES")){

如果确实如此,将不会继续str != null