当我使用int
共享偏好设置时,有人可以帮助我理解为什么会出现错误但是当我使用String
时它可以正常工作吗?
SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
int text = getPrefs.getInt("score", 3);
textView1.setText(text)
EDITED
这是我的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
答案 0 :(得分:2)
按如下方式设置textview文本:
textView1.setText(Integer.toString(text));
如果您只将int
作为参数传递给期望String
(或在这种情况下为CharSequence
)的函数,则需要指定转换为String,否则编译器不知道您是否尝试将其用作String,或者是否应该有一个函数setText()
,希望int
作为参数。
如果将int
与字符串组合在一起,例如setText("My value is: " + text);
,则不需要转换,因为编译器可以安全地假设您要将int
与指定的字符串连接起来
答案 1 :(得分:0)
您将int
传递给setText
,这需要CharSequence
,通常为String
。这不会编译。
您需要将int
转换为String
,通常是Integer.toString(myInt)
;
您遇到的运行时错误是因为基础值定义为String
。要么将其填充到其他位置String
,可能使用setString
,要么将其默认为String
。
答案 2 :(得分:0)
改变这个:
textView1.setText(text);
对此:
textView1.setText(String.valueOf(text));
答案 3 :(得分:0)
写这个
SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
int text = getPrefs.getInt("score", 3);
textView1.setText(Integer.toString(text));