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

String cannot be cast to Integer (incorrect library usage?) #40

Closed james-s-w-clark closed 6 years ago

james-s-w-clark commented 6 years ago

When I try to load preferences in my app, I keep getting the error "java.lang.RuntimeException: Unable to start activity ComponentInfo{.......MyPreferencesActivity}: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)"

In my activityMain I default the sharedPreference for a key value (upon first launch of the app): SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putInt("go_circle_colour_pref_key",Integer.parseInt("0x00ff00"));

In my preferences.xml, I have: <com.rarepebble.colorpicker.ColorPreference android:key="go_circle_colour_pref_key" android:title="@string/goColour" app:colorpicker_showAlpha="true" />

I have tried:

If I give the color-picker preference in xml a key that doesn't actually get used (initialised in activityMain), then the colour picker works OK. I seem to be initialising the key incorrectly.

martin-stone commented 6 years ago

Your code example gives me

java.lang.NumberFormatException: For input string: "0x00ff00"

But if I put...

    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putInt("simplePreference", 0xff00ffff).commit();

...into the onCreate() of the demo app, it works perfectly for me (setting the colour to opaque cyan).

If you need more help with this, I'll need to see more code.

james-s-w-clark commented 6 years ago

This fixed things - thanks! So the preference just had to be initialised with both alpha and rgb? I got the impression from the example code on the readme that just rgb was needed - but this was my mistake. I thought the value from "ColorPickerView usage" (....picker.setColor(0xff12345);...) would apply to initialising preferences.

edit: added PR which updates readme with SharedPreferences initialization at onCreate().