我正在尝试将listpreference添加到我的应用程序中。我的xml代码如下:
<?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>
我的数组值是:
<?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>
但是,当我尝试进入我的首选项菜单时,它会崩溃并出现以下异常:
10-03 20:27:56.938: ERROR/AndroidRuntime(1111): Caused by: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
这是指这行代码:
addPreferencesFromResource(R.xml.preferences);
对此有什么想法,为什么会这样?感谢。
完全堆积痕迹:
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)
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
答案 0 :(得分:15)
您之前在偏好设置中保留了Integer
。 ListActivity
期望String
投降失败。
只需在onCreate
的开头调用此代码即可删除错误的值,这应该修复:
PreferenceManager.getDefaultSharedPreferences(this).edit().remove("dayFilter").commit();
如果它仍然发生,那么您可能会使用dayFilter
两次作为不同偏好设置的密钥,或者在您的代码中的某个位置将{8}作为Integer
持久保存。
如果这不起作用,请尝试从以下位置删除您的偏好设置:
/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
如果确实失败了,请尝试卸载应用程序,以便将其删除(这不适用于我的Samsung Galaxy S - 共享偏好设置不会被删除...)
答案 1 :(得分:-1)
您的XML文件编写不正确,您声明了一个字符串数组,但是您提供了整数,因此导致了Cast异常。
<string-array name="dayFilterValues">
<item>0</item>
...
<item>365</item>
</string-array>
要修复它,只需在整数值周围加上引号:
<item>"365"</item>
然后在你的应用程序中,你需要将字符串解析成一个Integer:
int value = Integer.parseInt(str);