0

我试图从 Android 上的单个选择设置中获取相应的数字,由于某种原因,它在 LogCat 中告诉我“字符串不能在 getInt 处转换为整数。当我进入分析 sharedPref 的值时mEventLanguageSelection 分配的时间。在这一点上,它说KEY_LANGUAGE_SELECTION_LIST=2,我很确定这意味着 getInt 应该返回值 2。发生了什么?

EventsActivity.java
http://pastebin.com/85qa72bN

strings_activity_settings.xml
http://pastebin.com/LBVqpjMF

pref_general.xml
http://pastebin.com/NGcRwGhz

09-22 20:38:25.056: D/libEGL(28573): loaded /vendor/lib/egl/libEGL_POWERVR_SGX540_120.so
09-22 20:38:25.064: D/libEGL(28573): loaded /vendor/lib/egl/libGLESv1_CM_POWERVR_SGX540_120.so
09-22 20:38:25.071: D/libEGL(28573): loaded /vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so
09-22 20:38:25.181: D/OpenGLRenderer(28573): Enabling debug mode 0
09-22 20:38:33.157: D/dalvikvm(28573): GC_CONCURRENT freed 92K, 2% free 9117K/9240K, paused 3ms+2ms, total 25ms
09-22 20:39:33.759: D/AndroidRuntime(28573): Shutting down VM
09-22 20:39:33.759: W/dalvikvm(28573): threadid=1: thread exiting with uncaught exception (group=0x41415930)
09-22 20:39:33.790: E/AndroidRuntime(28573): FATAL EXCEPTION: main
09-22 20:39:33.790: E/AndroidRuntime(28573): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.barjinx.barjinx/com.barjinx.barjinx.EventsActivity}: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
09-22 20:39:33.790: E/AndroidRuntime(28573):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
09-22 20:39:33.790: E/AndroidRuntime(28573):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
09-22 20:39:33.790: E/AndroidRuntime(28573):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-22 20:39:33.790: E/AndroidRuntime(28573):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
09-22 20:39:33.790: E/AndroidRuntime(28573):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-22 20:39:33.790: E/AndroidRuntime(28573):    at android.os.Looper.loop(Looper.java:137)
09-22 20:39:33.790: E/AndroidRuntime(28573):    at android.app.ActivityThread.main(ActivityThread.java:5039)
09-22 20:39:33.790: E/AndroidRuntime(28573):    at java.lang.reflect.Method.invokeNative(Native Method)
09-22 20:39:33.790: E/AndroidRuntime(28573):    at java.lang.reflect.Method.invoke(Method.java:511)
09-22 20:39:33.790: E/AndroidRuntime(28573):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-22 20:39:33.790: E/AndroidRuntime(28573):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-22 20:39:33.790: E/AndroidRuntime(28573):    at dalvik.system.NativeStart.main(Native Method)
09-22 20:39:33.790: E/AndroidRuntime(28573): Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
09-22 20:39:33.790: E/AndroidRuntime(28573):    at android.app.SharedPreferencesImpl.getInt(SharedPreferencesImpl.java:240)
09-22 20:39:33.790: E/AndroidRuntime(28573):    at com.barjinx.barjinx.EventsActivity.onCreate(EventsActivity.java:65)
09-22 20:39:33.790: E/AndroidRuntime(28573):    at android.app.Activity.performCreate(Activity.java:5104)
09-22 20:39:33.790: E/AndroidRuntime(28573):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
09-22 20:39:33.790: E/AndroidRuntime(28573):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
09-22 20:39:33.790: E/AndroidRuntime(28573):    ... 11 more
4

2 回答 2

0

'android:key' 是字符串首选项,而不是整数。因此你应该使用sharedPref.getString("KEY_LANGUAGE_SELECTION_LIST", "");而不是getInt

文档

必须是字符串值,使用 '\;' 为 unicode 字符转义字符,例如 '\n' 或 '\uxxxx'。

于 2013-09-23T01:52:15.243 回答
0

我在整数对象的共享首选项中设置了字符串值“” ,所以当我使用 getInt() 从首选项中获取值时,我遇到了这个问题,所以请确保将值设置为仅整数,而不是字符串。

于 2017-12-06T14:04:24.037 回答