2013-11-23 50 views
0

有人可以幫助我理解爲什麼當我使用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 

回答

2

設置你的TextView文字如下:

textView1.setText(Integer.toString(text)); 

如果你只傳遞一個int作爲參數傳遞給函數預計String(或在此情況下爲CharSequence),則需要指定轉換爲字符串,否則編譯器不知道您是將它用作字符串還是應該有一個函數setText(),該函數需要int作爲參數。

如果您結合您的int用字符串,如setText("My value is: " + text);那麼你不需要轉換,因爲編譯器可以安全地假定你的意思是你的串聯與int指定的字符串。

+0

我想你對'parseInt'感到困惑。 –

+0

對!我的意思是'toString()'。修正了,謝謝。 –

+0

嘗試過,但當我打開包含代碼的活動時仍然出現錯誤 – John

0

您通過intsetText需要CharSequence,通常爲String。這不會編譯。

您需要將您的int轉換爲String,通常是通過Integer.toString(myInt);


您所遇到的運行時錯誤是因爲潛在價值被定義爲String。您可以將其作爲String填充到其他位置,可能使用setString或將其默認爲String

參見:SharedPreferences.Editor.putInt

0

更改此:

textView1.setText(text); 

要這樣:

textView1.setText(String.valueOf(text)); 
0

寫這

SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext()); 
    int text = getPrefs.getInt("score", 3); 
    textView1.setText(Integer.toString(text));