Android crashing on ListPreference
I'm trying to add a listpreference to my application. My xml code is the following:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="@string/calendarsettings">
<CheckBoxPreference android:key="syncWithCalendar"
android:title="@string/setting_calendarsync" android:summary="@string/setting_calendarsync"></CheckBoxPreference>
<CheckBoxPreference android:key="calendarAlarm"
android:title="@string/settings_calendaralarm" android:summary="@string/settings_calendaralarm"></CheckBoxPreference>
</PreferenceCategory>
<PreferenceCategory android:title="@string/filters">
<ListPreference android:key="dayFilter" android:title="@string/dayfilter"
android:summary="@string/dayfilter" android:entries="@array/dayFilterArray"
android:entryValues="@array/dayFilterValues" />
</PreferenceCategory>
</PreferenceScreen>
And my array values are:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="dayFilterArray">
<item>a</item>
<item>b</item>
<item>c</item>
<item>d</item>
<item>e</item>
<item>f</item>
<item>g</item>
</string-array>
<string-array name="dayFilterValues">
<item>0</item>
<item>3</item>
<item>7</item>
<item>14</item>
<item>30</item>
<item>90</item>
<item>365</item>
</string-array>
</resources>
However when I try to enter my preference menu it crashes with the following exception:
10-03 20:27:56.938: ERROR/AndroidRuntime(1111): Caused by: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
Which refers to this line of code:
addPreferencesFromResource(R.xml.preferences);
Any thoughts on this and why is it happening? Thanks.
FULL STACK TRACE:
10-03 21:13:06.828: ERROR/AndroidRuntime(3045): FATAL EXCEPTION: main
10-03 21:13:06.828: ERROR/AndroidRuntime(3045): java.lang.RuntimeException: Unable to start activity ComponentInfo{cPreferencesActivity}: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
10-03 21:13:06.828: ERROR/AndroidRuntime(3045): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1748)
10-03 21:13:06.828: ERROR/AndroidRuntime(3045): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1764)
10-03 21:13:06.828: ERROR/AndroidRuntime(3045): at android.app.ActivityThread.access$1500(ActivityThread.java:122)
开发者_Go百科10-03 21:13:06.828: ERROR/AndroidRuntime(3045): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1002)
10-03 21:13:06.828: ERROR/AndroidRuntime(3045): at android.os.Handler.dispatchMessage(Handler.java:99)
10-03 21:13:06.828: ERROR/AndroidRuntime(3045): at android.os.Looper.loop(Looper.java:132)
10-03 21:13:06.828: ERROR/AndroidRuntime(3045): at android.app.ActivityThread.main(ActivityThread.java:4025)
10-03 21:13:06.828: ERROR/AndroidRuntime(3045): at java.lang.reflect.Method.invokeNative(Native Method)
10-03 21:13:06.828: ERROR/AndroidRuntime(3045): at java.lang.reflect.Method.invoke(Method.java:491)
10-03 21:13:06.828: ERROR/AndroidRuntime(3045): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
10-03 21:13:06.828: ERROR/AndroidRuntime(3045): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
10-03 21:13:06.828: ERROR/AndroidRuntime(3045): at dalvik.system.NativeStart.main(Native Method)
10-03 21:13:06.828: ERROR/AndroidRuntime(3045): Caused by: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
10-03 21:13:06.828: ERROR/AndroidRuntime(3045): at android.app.SharedPreferencesImpl.getString(SharedPreferencesImpl.java:203)
10-03 21:13:06.828: ERROR/AndroidRuntime(3045): at android.preference.Preference.getPersistedString(Preference.java:1367)
10-03 21:13:06.828: ERROR/AndroidRuntime(3045): at android.preference.ListPreference.onSetInitialValue(ListPreference.java:278)
10-03 21:13:06.828: ERROR/AndroidRuntime(3045): at android.preference.Preference.dispatchSetInitialValue(Preference.java:1283)
10-03 21:13:06.828: ERROR/AndroidRuntime(3045): at android.preference.Preference.onAttachedToHierarchy(Preference.java:1095)
10-03 21:13:06.828: ERROR/AndroidRuntime(3045): at android.preference.PreferenceGroup.addPreference(PreferenceGroup.java:156)
10-03 21:13:06.828: ERROR/AndroidRuntime(3045): at android.preference.PreferenceGroup.addItemFromInflater(PreferenceGroup.java:97)
10-03 21:13:06.828: ERROR/AndroidRuntime(3045): at android.preference.PreferenceGroup.addItemFromInflater(PreferenceGroup.java:38)
10-03 21:13:06.828: ERROR/AndroidRuntime(3045): at android.preference.GenericInflater.rInflate(GenericInflater.java:488)
10-03 21:13:06.828: ERROR/AndroidRuntime(3045): at android.preference.GenericInflater.rInflate(GenericInflater.java:493)
10-03 21:13:06.828: ERROR/AndroidRuntime(3045): at android.preference.GenericInflater.inflate(GenericInflater.java:326)
10-03 21:13:06.828: ERROR/AndroidRuntime(3045): at android.preference.GenericInflater.inflate(GenericInflater.java:263)
10-03 21:13:06.828: ERROR/AndroidRuntime(3045): at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:269)
10-03 21:13:06.828: ERROR/AndroidRuntime(3045): at android.preference.PreferenceActivity.addPreferencesFromResource(PreferenceActivity.java:1333)
10-03 21:13:06.828: ERROR/AndroidRuntime(3045): at PreferencesActivity.onCreate(PreferencesActivity.java:33)
10-03 21:13:06.828: ERROR/AndroidRuntime(3045): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
10-03 21:13:06.828: ERROR/AndroidRuntime(3045): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1712)
10-03 21:13:06.828: ERROR/AndroidRuntime(3045): ... 11 more
You have persisted an Integer
before in the preferences. ListActivity
expects a String
so cast fails.
Just call this code at the beginning of onCreate
to remove the wrong value and this should be fixed:
PreferenceManager.getDefaultSharedPreferences(this).edit().remove("dayFilter").commit();
If it still happens than you're probably using the dayFilter
twice as key for different preferences or do persist that key on your own as Integer
somewhere in your code.
If this doesn't work then try to remove your preferences from this locations:
/data/data/YOUR_PACKAGE_NAME/shared_prefs/YOUR_PREFS_NAME.xml
/data/data/YOUR_PACKAGE_NAME/shared_prefs/YOUR_PACKAGE_NAME_preferences.xml
// on some devices
/dbdata/databases/YOUR_PACKAGE_NAME/shared_prefs/YOUR_PREFS_NAME.xml
If this does fail, try to uninstall application so they should be removed (this doesn't work on my Samsung Galaxy S - shared preferences are not deleted...)
Your XML file isn't written correctly, you declare an array of strings but you provide integers hence the Cast exception.
<string-array name="dayFilterValues">
<item>0</item>
...
<item>365</item>
</string-array>
To fix it simply put quotes around the integer values:
<item>"365"</item>
Then in your app you need to parse the string into an Integer with:
int value = Integer.parseInt(str);
精彩评论