有人可以幫助我理解爲什麼當我使用int
共享首選項時出現錯誤,但是當我使用String
時,它工作正常嗎?整數sharedPreferences錯誤
SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
int text = getPrefs.getInt("score", 3);
textView1.setText(text)
編輯
這裏是我的logcat
10-18 20:20:56.255: D/dalvikvm(6245): GC_FOR_MALLOC freed 2597 objects/165960 bytes in 120ms
10-18 20:20:56.625: D/AndroidRuntime(6245): Shutting down VM
10-18 20:20:56.625: W/dalvikvm(6245): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
10-18 20:20:56.647: E/AndroidRuntime(6245): FATAL EXCEPTION: main
10-18 20:20:56.647: E/AndroidRuntime(6245): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.thesis.logipic/com.thesis.logipic.Gameplay}: java.lang.ClassCastException: java.lang.String
10-18 20:20:56.647: E/AndroidRuntime(6245): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
10-18 20:20:56.647: E/AndroidRuntime(6245): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
10-18 20:20:56.647: E/AndroidRuntime(6245): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
10-18 20:20:56.647: E/AndroidRuntime(6245): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
10-18 20:20:56.647: E/AndroidRuntime(6245): at android.os.Handler.dispatchMessage(Handler.java:99)
10-18 20:20:56.647: E/AndroidRuntime(6245): at android.os.Looper.loop(Looper.java:123)
10-18 20:20:56.647: E/AndroidRuntime(6245): at android.app.ActivityThread.main(ActivityThread.java:4627)
10-18 20:20:56.647: E/AndroidRuntime(6245): at java.lang.reflect.Method.invokeNative(Native Method)
10-18 20:20:56.647: E/AndroidRuntime(6245): at java.lang.reflect.Method.invoke(Method.java:521)
10-18 20:20:56.647: E/AndroidRuntime(6245): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-18 20:20:56.647: E/AndroidRuntime(6245): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-18 20:20:56.647: E/AndroidRuntime(6245): at dalvik.system.NativeStart.main(Native Method)
10-18 20:20:56.647: E/AndroidRuntime(6245): Caused by: java.lang.ClassCastException: java.lang.String
10-18 20:20:56.647: E/AndroidRuntime(6245): at android.app.ContextImpl$SharedPreferencesImpl.getInt(ContextImpl.java:2706)
10-18 20:20:56.647: E/AndroidRuntime(6245): at com.thesis.logipic.Gameplay.onCreate(Gameplay.java:157)
10-18 20:20:56.647: E/AndroidRuntime(6245): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-18 20:20:56.647: E/AndroidRuntime(6245): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
10-18 20:20:56.647: E/AndroidRuntime(6245): ... 11 more
我想你對'parseInt'感到困惑。 –
對!我的意思是'toString()'。修正了,謝謝。 –
嘗試過,但當我打開包含代碼的活動時仍然出現錯誤 – John