I'm getting a very weird error.
I have 2 activities. On both I'm getting the SharedPreferences
using MODE_PRIVATE
(if it matters) by sp = getPreferences(MODE_PRIVATE);
on each activity's onCreate()
I'm calling sp.getBoolean(IntroActivity.SHOW_INTRO, true)
On the IntroActivity
this works fine. But when I'm trying in the main activity, I'm getting this
10-12 04:55:23.208: E/AndroidRuntime(11668): FATAL EXCEPTION: main
10-12 04:55:23.208: E/AndroidRuntime(11668): java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Boolean
10-12 04:55:23.208: E/AndroidRuntime(11668): at android.app.SharedPreferencesImpl.getBoolean(SharedPreferencesImpl.java:242)
10-12 04:55:23.208: E/AndroidRuntime(11668): at com.lablabla.parkme.ParkMeActivity$2.onClick(ParkMeActivity.java:194)
10-12 04:55:23.208: E/AndroidRuntime(11668): at android.view.View.performClick(View.java:4084)
10-12 04:55:23.208: E/AndroidRuntime(11668): at android.view.View$PerformClick.run(View.java:16966)
10-12 04:55:23.208: E/AndroidRuntime(11668): at android.os.Handler.handleCallback(Handler.java:615)
10-12 04:55:23.208: E/AndroidRuntime(11668): at android.os.Handler.dispatchMessage(Handler.java:92)
10-12 04:55:23.208: E/AndroidRuntime(11668): at android.os.Looper.loop(Looper.java:137)
10-12 04:55:23.208: E/AndroidRuntime(11668): at android.app.ActivityThread.main(ActivityThread.java:4745)
10-12 04:55:23.208: E/AndroidRuntime(11668): at java.lang.reflect.Method.invokeNative(Native Method)
10-12 04:55:23.208: E/AndroidRuntime(11668): at java.lang.reflect.Method.invoke(Method.java:511)
10-12 04:55:23.208: E/AndroidRuntime(11668): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-12 04:55:23.208: E/AndroidRuntime(11668): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-12 04:55:23.208: E/AndroidRuntime(11668): at dalvik.system.NativeStart.main(Native Method)
I made sure that I'm not putting a String
somewhere in the middle with that same key
Any ideas?
Thanks!
EDIT: some code:
//onCreate()
sp = getPreferences(MODE_PRIVATE);
// other method
boolean showIntro = sp.getBoolean(IntroActivity.SHOW_INTRO, true); // Exception is here
showIntroCheckBox.setChecked(showIntro);
If it matters, the code which throws the exception is inside a button's onClick
To convert String to Boolean, use the parseBoolean() method in Java. The parseBoolean() parses the string argument as a boolean. The boolean returned represents the value true if the string argument is not null and is equal, ignoring case, to the string "true".
To convert String to boolean in Java, you can use Boolean. parseBoolean(string). But if you want to convert String to Boolean object then use the method Boolean. valueOf(string) method.
To convert Boolean to String in Java, use the toString() method. For this, firstly, we have declared two booleans. String str1 = new Boolean(bool1). toString(); String str2 = new Boolean(bool2).
There are two ways to convert a String to a boolean in Java, first, by using Boolean. parseBoolean() method, and second, by using Boolean. valueOf() method. The parseBoolean() method returns an equivalent boolean value of a given String, for example, if you pass "true" it will return the primitive boolean value true.
If there's ever been a string with that key, even if by accident, it will stay there until you clear the app's data or uninstall. Try uninstalling it to see if it still occurs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With