Android crash on ListPreference

I am trying to add a listpreference to my application. My xml code is as follows:

<?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 preferences 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 

What relates to this line of code:

  addPreferencesFromResource(R.xml.preferences); 

Any thoughts on this and why is this 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) 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 
+6
source share
2 answers

You saved Integer before in settings. ListActivity expects a String , so the failure is in ListActivity .

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 this still happens, you are probably using dayFilter twice as a key for different settings, or save that key yourself as an Integer somewhere in your code.

If this does not work, try deleting your settings from these places:

 /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 fails, try uninstalling the application so that they are deleted (this does not work on my Samsung Galaxy S - general settings are not deleted ...)

+15
source

Your XML file is not 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 this, simply put quotation marks around integer values:

 <item>"365"</item> 

Then in your application you need to parse the string in Integer with:

 int value = Integer.parseInt(str); 
-1
source

Source: https://habr.com/ru/post/898547/


All Articles