/ / SharedPreference - wyjątek Null Pointer - android, sharedpreferences

SharedPreference - wyjątek wskaźnika pustego - android, sharedpreferences

Próbuję zapisać coś w SharedPreferences przy użyciu tego kodu poniżej:

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

}

Przeczytałem i zapoznałem się z kilkoma samouczkami w Internecie, ale poniżej pojawia się błąd. To jest 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

Daj mi znać, co jest nie tak z kodem, którego używam, zawsze mam siłę. Dzięki

Odpowiedzi:

8 dla odpowiedzi № 1
str = PreferenceManager.getDefaultSharedPreferences(this).getString(
"signedUp", null);

Twoja zmienna str ma wartość NULL, ponieważ wartość „podpisana” nie miała wcześniej ustawionej wartości. Sprawdź, czy w instrukcji if nie ma wartości null.

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

Jeśli nie będzie kontynuować str != null Jeśli to prawda.