0

I'm trying to set the color of a view which is stored in sharedpreference. But when I try to parse the color string, it throws ClassCastException.

Here is my code.

        SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
        String textColor = sharedPref.getString("textColor", "#00ff00");
        String backgroundColor = sharedPref.getString("backgroundColor", "#000000");
        digitalClock.setTextColor(Color.parseColor(textColor));

According to other stackoverflow posts, I have to parse the color string to Color integer using following code.

Color.parseColor(textColor)

But it doesn't output integer value.

Here is the logcat output.

11-16 23:04:49.009 15566-15566/? E/AndroidRuntime: FATAL EXCEPTION: main
11-16 23:04:49.009 15566-15566/? E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{uk.co.andromedatech.digitalclock/uk.co.andromedatech.digitalclock.MainActivity}: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
11-16 23:04:49.009 15566-15566/? E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
11-16 23:04:49.009 15566-15566/? E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
11-16 23:04:49.009 15566-15566/? E/AndroidRuntime:     at android.app.ActivityThread.access$600(ActivityThread.java:127)
11-16 23:04:49.009 15566-15566/? E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
11-16 23:04:49.009 15566-15566/? E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:99)
11-16 23:04:49.009 15566-15566/? E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:137)
11-16 23:04:49.009 15566-15566/? E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:4441)
11-16 23:04:49.009 15566-15566/? E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
11-16 23:04:49.009 15566-15566/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:511)
11-16 23:04:49.009 15566-15566/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-16 23:04:49.009 15566-15566/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-16 23:04:49.009 15566-15566/? E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)
11-16 23:04:49.009 15566-15566/? E/AndroidRuntime:  Caused by: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
11-16 23:04:49.009 15566-15566/? E/AndroidRuntime:     at android.app.SharedPreferencesImpl.getString(SharedPreferencesImpl.java:205)
11-16 23:04:49.009 15566-15566/? E/AndroidRuntime:     at uk.co.andromedatech.digitalclock.MainActivity.onCreate(MainActivity.java:29)
11-16 23:04:49.009 15566-15566/? E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:4465)
11-16 23:04:49.009 15566-15566/? E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
11-16 23:04:49.009 15566-15566/? E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
11-16 23:04:49.009 15566-15566/? E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992) 
11-16 23:04:49.009 15566-15566/? E/AndroidRuntime:     at android.app.ActivityThread.access$600(ActivityThread.java:127) 
11-16 23:04:49.009 15566-15566/? E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158) 
11-16 23:04:49.009 15566-15566/? E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:99) 
11-16 23:04:49.009 15566-15566/? E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:137) 
11-16 23:04:49.009 15566-15566/? E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:4441) 
11-16 23:04:49.009 15566-15566/? E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method) 
11-16 23:04:49.009 15566-15566/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:511) 
11-16 23:04:49.009 15566-15566/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
11-16 23:04:49.009 15566-15566/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
11-16 23:04:49.009 15566-15566/? E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method) 
11-16 23:04:50.183 15566-15566/? I/Process: Sending signal. PID: 15566 SIG: 9
Community
  • 1
  • 1
Isuru
  • 3,818
  • 13
  • 49
  • 64

1 Answers1

3
E/AndroidRuntime:  Caused by: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
E/AndroidRuntime:     at android.app.SharedPreferencesImpl.getString(SharedPreferencesImpl.java:205)
E/AndroidRuntime:     at uk.co.andromedatech.digitalclock.MainActivity.onCreate(MainActivity.java:29)

Your getString() call is what is failing, because SharedPreferences thinks that the preference is an integer, not a string. Either:

  • You are storing the preference via putInt(), or

  • The syntax of # followed by hex digits is being used by SharedPreferences for encoding integers, even though you may be setting the value via putString()

If you are storing the value via putInt(), either switch to putString() or getInt().

If you are already storing the value via putString(), then my latter scenario is more likely, and you might consider just switching to putInt() and getInt().

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Well, I use PreferenceScreen to store that value. Following is the code, – Isuru Nov 16 '15 at 23:23
  • @Isuru: If [this is the source code](https://github.com/martin-stone/hsv-alpha-color-picker-android/blob/master/colorpicker/src/main/java/com/rarepebble/colorpicker/ColorPreference.java) to what you are using, it is saving the preference as an integer. Change your code to use `getInt()` instead of `getString()`. – CommonsWare Nov 16 '15 at 23:28
  • 1
    @MikeM.: Yes, thanks, though as it turns out from the earlier comments, that doesn't appear to be the case anyway. – CommonsWare Nov 16 '15 at 23:36
  • Thanks! @CommonsWare. That was the problem. Anyway your book is the best. Learned lot from it. – Isuru Nov 17 '15 at 00:06