我正在尝试设置存储在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整数。
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
答案 0 :(得分: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)
您的getString()
调用失败了,因为SharedPreferences
认为首选项是整数,而不是字符串。之一:
您正在通过putInt()
或
#
后跟十六进制数字的语法正由SharedPreferences
用于编码整数,即使您可能通过putString()
设置值
如果您要通过putInt()
存储值,请切换为putString()
或getInt()
。
如果您已经通过putString()
存储了该值,则后一种情况更有可能发生,您可能会考虑切换到putInt()
和getInt()
。