javaandroidcastingsharedpreferenceslistpreference

Read of Integer in Shared Preferences causes Class Cast exception


I am trying to obtain the corresponding number from a single select setting on Android, and for some reason it tells me in the LogCat that "String cannot be cast to Integer at getInt. When I go in to analyze the value of the sharedPref at the time of mEventLanguageSelection assignment. At that point, it says KEY_LANGUAGE_SELECTION_LIST=2, which I am quite certain means that getInt should return a value of 2. What is happening?

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

Solution

  • The 'android:key' is a String Preference and not an integer. Hence you should be using sharedPref.getString("KEY_LANGUAGE_SELECTION_LIST", ""); and not getInt

    From the docs

    Must be a string value, using '\;' to escape characters such as '\n' or '\uxxxx' for a unicode character.