ClassCastException in PreferenceActivity

若如初见. 提交于 2019-11-29 13:55:38

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.

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!