martin-stone / hsv-alpha-color-picker-android

A color picker and a color preference for use in Android applications.
Apache License 2.0
290 stars 60 forks source link

ClassCastException when migrating from EditTextPreference #36

Closed dejj closed 7 years ago

dejj commented 7 years ago

When replacing an EditTextPreference:

<EditTextPreference
    android:defaultValue="@color/my_default_color"
    android:key="my_bgcolor"
    android:selectAllOnFocus="true"
    android:singleLine="true"
    android:title="@string/my_color_title"/>

with ColorPreference:

<com.rarepebble.colorpicker.ColorPreference
    android:key="my_bgcolor"
    android:title="@string/my_color_title"
    android:defaultValue="@color/my_default_color"/>

I receive this exception upon opening the preference page:

Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
  at android.app.SharedPreferencesImpl.getInt(SharedPreferencesImpl.java:239)
  at android.preference.Preference.getPersistedInt(Preference.java:1559)
  at com.rarepebble.colorpicker.ColorPreference.getPersistedIntDefaultOrNull(ColorPreference.java:120)
  at com.rarepebble.colorpicker.ColorPreference.getColor(ColorPreference.java:199)
  at com.rarepebble.colorpicker.ColorPreference.onSetInitialValue(ColorPreference.java:104)
  at android.preference.Preference.dispatchSetInitialValue(Preference.java:1376)
  ...

The android:key="my_bgcolor" seems to store a dynamic type of String for EditTextPreference, which ColorPreference tries to cast into Integer.

A workaround is to rename the key or uninstall the app.

martin-stone commented 7 years ago

Thanks for the report, but I'm happy to let the library fail on this. You're asking the color preference to read a setting that it didn't store. In a released app this shouldn't occur as users won't be changing the type of the saved preference value. The built-in Android preferences do the same thing (e.g. changing to an EditTextPreference from a preference that stores an integer).