ClassCastException in PreferenceActivity

前端 未结 3 1420
野的像风
野的像风 2020-12-20 20:07

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

3条回答
  •  南方客
    南方客 (楼主)
    2020-12-20 20:19

    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.

提交回复
热议问题