我正在尝试设置存储在sharedpreference中的视图的颜色。但是,当我尝试解析颜色字符串时,它将引发ClassCastException。

这是我的代码。

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


根据其他stackoverflow posts,我必须使用以下代码将颜色字符串解析为Color integer。

Color.parseColor(textColor)


但是它不会输出整数值。

这是logcat的输出。

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

最佳答案

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)


您的getString()调用失败了,因为SharedPreferences认为首选项是整数,而不是字符串。要么:


您正在通过putInt()存储首选项,或者
即使可以通过#设置值,SharedPreferences仍使用putString()后面跟十六进制数字的语法对整数进行编码。


如果要通过putInt()存储值,请切换到putString()getInt()

如果您已经通过putString()存储值,那么我的后一种情况更有可能出现,您可以考虑仅切换到putInt()getInt()

07-24 14:25