ClassCastException in PreferenceActivity

I am trying to get an example from the Android 2 application development book from Reto Meier (p. 202). Following the instructions, I created userpreferences.xml as follows:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" > <CheckBoxPreference android:key="PREF_AUTO_UPDATE" android:title="Auto refresh" android:summary="Select to turn on automatic updating" android:defaultValue="true" /> <ListPreference android:key="PREF_UPDATE_FREQ" android:title="Refresh frequency" android:summary="Frequency at which to refresh earthquake list" android:entries="@array/update_freq_options" android:entryValues="@array/update_freq_values" android:dialogTitle="Refresh frequency" android:defaultValue="60" /> <ListPreference android:key="PREF_MIN_MAG" android:title="Minimum magnitude" android:summary="Select the minimum magnitude earthquake to report" android:entries="@array/magnitude_options" android:entryValues="@array/magnitude" android:dialogTitle="Magnitude" android:defaultValue="3" /> </PreferenceScreen> 

The My Preferences class is as follows:

 public class Preferences extends PreferenceActivity { SharedPreferences prefs; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.userpreferences); } 

I keep getting ClassCastException: java.lang.ClassCastException: java.lang.Integer

 12-16 09:28:14.349: ERROR/AndroidRuntime(287): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dt.sample/com.dt.sample.Preferences}: java.lang.ClassCastException: java.lang.Integer 12-16 09:28:14.349: ERROR/AndroidRuntime(287): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 12-16 09:28:14.349: ERROR/AndroidRuntime(287): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 12-16 09:28:14.349: ERROR/AndroidRuntime(287): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 12-16 09:28:14.349: ERROR/AndroidRuntime(287): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 12-16 09:28:14.349: ERROR/AndroidRuntime(287): at android.os.Handler.dispatchMessage(Handler.java:99) 12-16 09:28:14.349: ERROR/AndroidRuntime(287): at android.os.Looper.loop(Looper.java:123) 12-16 09:28:14.349: ERROR/AndroidRuntime(287): at android.app.ActivityThread.main(ActivityThread.java:4627) 12-16 09:28:14.349: ERROR/AndroidRuntime(287): at java.lang.reflect.Method.invokeNative(Native Method) 12-16 09:28:14.349: ERROR/AndroidRuntime(287): at java.lang.reflect.Method.invoke(Method.java:521) 12-16 09:28:14.349: ERROR/AndroidRuntime(287): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 12-16 09:28:14.349: ERROR/AndroidRuntime(287): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 12-16 09:28:14.349: ERROR/AndroidRuntime(287): at dalvik.system.NativeStart.main(Native Method) 12-16 09:28:14.349: ERROR/AndroidRuntime(287): Caused by: java.lang.ClassCastException: java.lang.Integer 12-16 09:28:14.349: ERROR/AndroidRuntime(287): at android.app.ContextImpl$SharedPreferencesImpl.getString(ContextImpl.java:2699) 12-16 09:28:14.349: ERROR/AndroidRuntime(287): at android.preference.Preference.getPersistedString(Preference.java:1249) 12-16 09:28:14.349: ERROR/AndroidRuntime(287): at android.preference.ListPreference.onSetInitialValue(ListPreference.java:232) 12-16 09:28:14.349: ERROR/AndroidRuntime(287): at android.preference.Preference.dispatchSetInitialValue(Preference.java:1172) 12-16 09:28:14.349: ERROR/AndroidRuntime(287): at android.preference.Preference.onAttachedToHierarchy(Preference.java:984) 12-16 09:28:14.349: ERROR/AndroidRuntime(287): at android.preference.PreferenceGroup.addPreference(PreferenceGroup.java:156) 12-16 09:28:14.349: ERROR/AndroidRuntime(287): at android.preference.PreferenceGroup.addItemFromInflater(PreferenceGroup.java:97) 12-16 09:28:14.349: ERROR/AndroidRuntime(287): at android.preference.PreferenceGroup.addItemFromInflater(PreferenceGroup.java:38) 12-16 09:28:14.349: ERROR/AndroidRuntime(287): at android.preference.GenericInflater.rInflate(GenericInflater.java:488) 12-16 09:28:14.349: ERROR/AndroidRuntime(287): at android.preference.GenericInflater.inflate(GenericInflater.java:326) 12-16 09:28:14.349: ERROR/AndroidRuntime(287): at android.preference.GenericInflater.inflate(GenericInflater.java:263) 12-16 09:28:14.349: ERROR/AndroidRuntime(287): at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:251) 12-16 09:28:14.349: ERROR/AndroidRuntime(287): at android.preference.PreferenceActivity.addPreferencesFromResource(PreferenceActivity.java:262) 12-16 09:28:14.349: ERROR/AndroidRuntime(287): at com.dt.sample.Preferences.onCreate(Preferences.java:24) 12-16 09:28:14.349: ERROR/AndroidRuntime(287): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 12-16 09:28:14.349: ERROR/AndroidRuntime(287): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 12-16 09:28:14.349: ERROR/AndroidRuntime(287): ... 11 more 

The arrays.xml file looks like this:

 <resources> <string-array name="update_freq_options"> <item>Every Minute</item> <item>5 minutes</item> <item>10 minutes</item> <item>15 minutes</item> <item>Every Hour</item> </string-array> <array name="magnitude"> <item>3</item> <item>5</item> <item>6</item> <item>7</item> <item>8</item> </array> <string-array name="magnitude_options"> <item>3</item> <item>5</item> <item>6</item> <item>7</item> <item>8</item> </string-array> <array name="update_freq_values"> <item>1</item> <item>5</item> <item>10</item> <item>15</item> <item>60</item> </array> </resources> 

I tried using an integer array, but that did not help. What am I doing wrong? Please help. Thanks.

+6
android android-preferences
source share
3 answers

I had the same problem - the user preference data from the previous example in the book used different data types when saving preference data.

The solution is to simply check the "Clear user data" checkbox when starting AVD to get rid of the old user settings.

+11
source share

If you change the type of preferences from ListPreference to CheckBoxPreference when reusing the same key, then this error will occur.

The Android database will store some default data in the applicationโ€™s share_prefs / preferences.xml file. These old values โ€‹โ€‹will be in the old format (for example, Int or String for ListPreference) instead of Boolean (for CheckBoxPreference).

When you load your preference activity, it will automatically load this XML file and cause this crash.

The solution is to simply edit this saved XML preference file (shared_prefs / preferences.xml) and delete the old values. Or just delete this XML file.

+6
source share

Perhaps you come across this? http://code.google.com/p/android/issues/detail?id=2096

[update] In the comment, yes, make all your arrays of type string-array, and you should be good.

+1
source share

All Articles