所以我有一個很奇怪的問題,可能很容易解決其他人,但我;-) 無論如何,我有很多活動,並在每個活動,我必須得到它的問題正確恢復,每個活動只有一個。java.lang.NullPointerException SharedPreferences getSharedPreferences
每當我開始一個活動,或把這一個活動睡覺,並立即返回它,它工作正常。但是,當我把它放到睡眠點擊主頁按鈕並啓動一堆不同的應用程序,然後嘗試再次返回它,我得到了getSharedPreferences java.lang.NullPointerException。
這怎麼會發生? SharedPreferences是否被刪除?
這裏是我的方法的getPreferences當錯誤發生:
private static SharedPreferences getPreferences() {
Context applicationContext = App.getContextOfApplication();
return applicationContext.getSharedPreferences(PREFS_USER, Context.MODE_PRIVATE);
}
這裏是我的logcat:
05-16 20:37:36.461: E/AndroidRuntime(13767): FATAL EXCEPTION: main
05-16 20:37:36.461: E/AndroidRuntime(13767): Process: com.test, PID: 13767
05-16 20:37:36.461: E/AndroidRuntime(13767): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test/com.test.MessageActivity}: java.lang.NullPointerException
05-16 20:37:36.461: E/AndroidRuntime(13767): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
05-16 20:37:36.461: E/AndroidRuntime(13767): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
05-16 20:37:36.461: E/AndroidRuntime(13767): at android.app.ActivityThread.access$900(ActivityThread.java:161)
05-16 20:37:36.461: E/AndroidRuntime(13767): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
05-16 20:37:36.461: E/AndroidRuntime(13767): at android.os.Handler.dispatchMessage(Handler.java:102)
05-16 20:37:36.461: E/AndroidRuntime(13767): at android.os.Looper.loop(Looper.java:157)
05-16 20:37:36.461: E/AndroidRuntime(13767): at android.app.ActivityThread.main(ActivityThread.java:5356)
05-16 20:37:36.461: E/AndroidRuntime(13767): at java.lang.reflect.Method.invokeNative(Native Method)
05-16 20:37:36.461: E/AndroidRuntime(13767): at java.lang.reflect.Method.invoke(Method.java:515)
05-16 20:37:36.461: E/AndroidRuntime(13767): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
05-16 20:37:36.461: E/AndroidRuntime(13767): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
05-16 20:37:36.461: E/AndroidRuntime(13767): at dalvik.system.NativeStart.main(Native Method)
05-16 20:37:36.461: E/AndroidRuntime(13767): Caused by: java.lang.NullPointerException
05-16 20:37:36.461: E/AndroidRuntime(13767): at com.test.LocalStorage.getPreferences(LocalStorage.java:30)
05-16 20:37:36.461: E/AndroidRuntime(13767): at com.test.LocalStorage.getUsername(LocalStorage.java:83)
05-16 20:37:36.461: E/AndroidRuntime(13767): at com.test.WebApi.getChat(WebApi.java:711)
05-16 20:37:36.461: E/AndroidRuntime(13767): at com.test.MessageActivity.onCreate(MessageActivity.java:100)
05-16 20:37:36.461: E/AndroidRuntime(13767): at android.app.Activity.performCreate(Activity.java:5426)
05-16 20:37:36.461: E/AndroidRuntime(13767): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
05-16 20:37:36.461: E/AndroidRuntime(13767): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
05-16 20:37:36.461: E/AndroidRuntime(13767): ... 11 more
在此先感謝!
''的applicationContext似乎是'null'。請顯示'App.getContextOfApplication()'的實現。 – cybersam
不要嘗試獲取/存儲對「Context」的靜態引用。每當你這樣做,一隻小貓在這種情況下死亡( – nhaarman