Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
james-s-w-clark opened this issue Dec 23, 2017 · 2 comments
Closed

Comments

@james-s-w-clark
Copy link
Contributor

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:

  • putting default colours in the preferences.xml, e.g.
    <!--android:defaultValue="#f00"-->

  • editor.putInt("go_circle_colour_pref_key",Integer.parseInt("0x00ff00"));

  • editor.putInt("go_circle_colour_pref_key",0x00ff00);

  • editor.putInt("go_circle_colour_pref_key",Color.parseColor("#00ff00");

  • editor.putString("go_circle_colour_pref_key","#00ff00";

  • editor.putString("go_circle_colour_pref_key","#0f0";

  • editor.putString("go_circle_colour_pref_key","0x00ff00";

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
Copy link
Owner

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
Copy link
Contributor Author

james-s-w-clark commented Dec 28, 2017

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().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants