1

我正在尝试使用以下代码将某些内容保存到 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!", ":)");
    }

}

我已阅读并遵循互联网上的一些教程,但出现以下错误。这是日志:

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

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

4

1 回答 1

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

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

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

str != null如果它是真的,它就不会继续过去。

于 2012-10-23T01:58:22.253 回答