2016-07-14 79 views
0
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this); 
    SharedPreferences.Editor editor = pref.edit(); 
    float highscore = pref.getFloat("highscore",0.0f); 

只要我輸入使用此部分的活動,應用程序就會崩潰。根據logcat,該代碼塊中的第一行給出了一個nullpointerException。如果它很重要,這部分是在OnCreate之後,我正在使用Android Studio。如果沒有android應用程序崩潰,我無法使用sharedpreferences

Logcat:FATAL EXCEPTION: main 
                       Process: com.nonexistent.rs.guessthenumber, PID: 4388 
                       java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.nonexistent.rs.guessthenumber/com.nonexistent.rs.guessthenumber.main}: java.lang.NullPointerException 
                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2306) 
                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471) 
                        at android.app.ActivityThread.access$900(ActivityThread.java:175) 
                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308) 
                        at android.os.Handler.dispatchMessage(Handler.java:102) 
                        at android.os.Looper.loop(Looper.java:146) 
                        at android.app.ActivityThread.main(ActivityThread.java:5602) 
                        at java.lang.reflect.Method.invokeNative(Native Method) 
                        at java.lang.reflect.Method.invoke(Method.java:515) 
                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283) 
                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099) 
                        at dalvik.system.NativeStart.main(Native Method) 
                        Caused by: java.lang.NullPointerException 
                        at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:185) 
                        at com.nonexistent.rs.guessthenumber.main.<init>(main.java:141) 
                        at java.lang.Class.newInstanceImpl(Native Method) 
                        at java.lang.Class.newInstance(Class.java:1208) 
                        at android.app.Instrumentation.newActivity(Instrumentation.java:1067) 
                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2297) 
                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)  
                        at android.app.ActivityThread.access$900(ActivityThread.java:175)  
                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)  
                        at android.os.Handler.dispatchMessage(Handler.java:102)  
                        at android.os.Looper.loop(Looper.java:146)  
                        at android.app.ActivityThread.main(ActivityThread.java:5602)  
                        at java.lang.reflect.Method.invokeNative(Native Method)  
                        at java.lang.reflect.Method.invoke(Method.java:515)  
                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)  
                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)  
                        at dalvik.system.NativeStart.main(Native Method)  
07-14 10:17:04.616 6030-6030/com.nonexistent.rs.guessthenumber E/AndroidRuntime: FATAL EXCEPTION: main 
                       Process: com.nonexistent.rs.guessthenumber, PID: 6030 
                       java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.nonexistent.rs.guessthenumber/com.nonexistent.rs.guessthenumber.main}: java.lang.NullPointerException 
                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2306) 
                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471) 
                        at android.app.ActivityThread.access$900(ActivityThread.java:175) 
                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308) 
                        at android.os.Handler.dispatchMessage(Handler.java:102) 
                        at android.os.Looper.loop(Looper.java:146) 
                        at android.app.ActivityThread.main(ActivityThread.java:5602) 
                        at java.lang.reflect.Method.invokeNative(Native Method) 
                        at java.lang.reflect.Method.invoke(Method.java:515) 
                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283) 
                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099) 
                        at dalvik.system.NativeStart.main(Native Method) 
                        Caused by: java.lang.NullPointerException 
                        at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:185) 
                        at android.preference.PreferenceManager.getDefaultSharedPreferences(PreferenceManager.java:369) 
                        at com.nonexistent.rs.guessthenumber.main.<init>(main.java:142) 
                        at java.lang.Class.newInstanceImpl(Native Method) 
                        at java.lang.Class.newInstance(Class.java:1208) 
                        at android.app.Instrumentation.newActivity(Instrumentation.java:1067) 
                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2297) 
                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)  
                        at android.app.ActivityThread.access$900(ActivityThread.java:175)  
                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)  
                        at android.os.Handler.dispatchMessage(Handler.java:102)  
                        at android.os.Looper.loop(Looper.java:146)  
                        at android.app.ActivityThread.main(ActivityThread.java:5602)  
                        at java.lang.reflect.Method.invokeNative(Native Method)  
                        at java.lang.reflect.Method.invoke(Method.java:515)  
                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)  
                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)  
                        at dalvik.system.NativeStart.main(Native Method)  
+1

你能指出你正在獲取空指針的確切位置嗎?如果可能的話添加完整的代碼。 –

+1

[使用LogCat檢查與您的崩潰相關的Java堆棧跟蹤](https://stackoverflow.com/questions/23353173/uncomfort-myapp-has-stopped-how-can-i-solve-this)。如果您不瞭解崩潰的觸發器,請發佈整個Java堆棧跟蹤。 – CommonsWare

+0

我放的代碼片段的第一行給出了一個空指針。我不認爲我需要把完整的代碼,因爲擺脫代碼片段消除了這個問題。 – superchampionoftheworld

回答

0

原因:

可能是你正在使用此代碼片段或其他線程。您需要將活動上下文引用作爲參數傳遞。論點this是問題所在。

解決方案:

你需要傳遞活動/上下文參數

SharedPreferences pref = 
PreferenceManager.getDefaultSharedPreferences(Your_Activity_Name.this); 
+0

它沒有工作。我仍然得到相同的錯誤,並崩潰。 – superchampionoftheworld

+0

好的,你在哪裏使用代碼?在一個活動或其他線程實體? –

+0

在活動上。 – superchampionoftheworld

-1

我很早以前就解決了這個自己,但後來我意識到,這是沒有答案。顯然,我應該把所有SharedPrefences代碼放在onCreate中。

+0

這是不正確的。您可以從任何地方使用共享偏好。你嘗試使用應用程序上下文嗎? – Hitesh

+0

我的意思是實際定義SP的部分。出於某種原因,只要我將其移至onCreate,我就沒有任何錯誤。 – superchampionoftheworld

相關問題