Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ClassCastException in PreferenceActivity

I am trying to get an example from the Android 2 Application Development book by Reto Meier to work (page 202). As per the instructions I have created a 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>

My Preferences class looks like this...

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

I keep getting a 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 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 integer-array but that didn't help. What am I doing wrong?

like image 455
DilTeam Avatar asked Dec 16 '10 18:12

DilTeam


3 Answers

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

The solution is to simply check the 'clear user data' checkbox when starting the AVD to get rid of the old user preferences.

like image 184
Matt Avatar answered Sep 28 '22 09:09

Matt


If you change a preference type from ListPreference to CheckBoxPreference, whilst reusing the same key, then this bug will happen.

The Android framework will store some default data in your app's shared_prefs/preferences.xml file. These old values will be in the old format (such as Int or String, for ListPreference) instead of Boolean (for CheckBoxPreference).

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

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

like image 25
user2554058 Avatar answered Sep 28 '22 08:09

user2554058


Perhaps you are running into this? http://code.google.com/p/android/issues/detail?id=2096.

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

like image 31
Andrew White Avatar answered Sep 28 '22 08:09

Andrew White