1

I am trying to get and set a boolean through Sharedpreferences. The following code perfectly works on Marshmallow but not on Lolipop (5.0.1) - I get an exception: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Boolean when I call getintroStatus()

Any help?

public void setIntrodone(boolean status) {
        editor = settings.edit();
        editor.putBoolean("intro_done_boolean", status);
        editor.commit();
    }

    public boolean getintroStatus() {
        try {
            return settings.getBoolean("intro_done_boolean", false);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }

Logcat Error:

java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Boolean
04-20 14:06:59.438 1401-1401/io.authme.home W/System.err:     at android.app.SharedPreferencesImpl.getBoolean(SharedPreferencesImpl.java:263)
04-20 14:06:59.438 1401-1401/io.authme.home W/System.err:     at io.authme.home.MitroApplication.getintroStatus(MitroApplication.java:107)
04-20 14:06:59.438 1401-1401/io.authme.home W/System.err:     at io.authme.home.MainActivity.onCreate(MainActivity.java:81)
04-20 14:06:59.438 1401-1401/io.authme.home W/System.err:     at android.app.Activity.performCreate(Activity.java:6285)
04-20 14:06:59.438 1401-1401/io.authme.home W/System.err:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
04-20 14:06:59.438 1401-1401/io.authme.home W/System.err:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2405)
04-20 14:06:59.438 1401-1401/io.authme.home W/System.err:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2535)
04-20 14:06:59.438 1401-1401/io.authme.home W/System.err:     at android.app.ActivityThread.access$900(ActivityThread.java:154)
04-20 14:06:59.438 1401-1401/io.authme.home W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1380)
04-20 14:06:59.438 1401-1401/io.authme.home W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
04-20 14:06:59.438 1401-1401/io.authme.home W/System.err:     at android.os.Looper.loop(Looper.java:148)
04-20 14:06:59.438 1401-1401/io.authme.home W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5490)
04-20 14:06:59.438 1401-1401/io.authme.home W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
04-20 14:06:59.438 1401-1401/io.authme.home W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
04-20 14:06:59.438 1401-1401/io.authme.home W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
  • 1
    please add you logcat/error log – Secret Coder Apr 20 '16 at 8:36
  • done........... – user1051505 Apr 20 '16 at 8:38
  • see stackoverflow.com/questions/16329426/… – pRaNaY Apr 20 '16 at 8:39
  • what is the code in line .getBoolean(SharedPreferencesImpl.java:263) – Linh Apr 20 '16 at 8:41
  • Do you have another preference with the same name? Or have you stored a preference with the same name previously? Try uninstall / delete preferences and reinstall your app. – Christopher Apr 20 '16 at 8:47
2

It seem to me that it doesnt have anything to do with the android version, maybe that other device had preference with the same key set to some String and that causes you problems.

Id suggest that you make sure you dont use that same key somewhere else, if you are sure you dont, id suggest that you clear your app data on the other device and try again.

1

Saw this in SharedPreferences.getBoolean documentation :

Returns the preference value if it exists, or defValue. Throws ClassCastException if there is a preference with this name that is not a boolean.

Maybe you used this key before to store a value that wasn't a boolean ? This might seems obvious, but have you tried to manually uninstall your app before running it again ?

Also, what is your minSDK (in your build.gradle) ? You might want to lower it to 21 (see API Levels here) if that's not the case. You could be using something somewhere else in your project, related to your preferences, that Android 5.0 cannot run.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.