Skip to content Skip to sidebar Skip to footer

Preference Cannot Cast Java.lang.boolean To String

I have the following Preference class: public class AppPreferencesActivity extends PreferenceActivity { private SharedPreferences appPrefs; private SharedPreferences.Editor

Solution 1:

I was getting the same error. I went and deleted App's data and my app doesn't crash anymore.


Solution 2:

It obviously that some prefs are checked and only return boolean value. So you cannot use this directly.

for (Entry<String, ?> key : appPrefs.getAll().entrySet()) {
    findPreference(key.getKey()).setSummary(appPrefs.getString(key.getKey(), "Not yet entered"));
}

I think maybe you should use like that:

for (Entry<String, ?> key : appPrefs.getAll().entrySet()) {
    Object result = key.getValue();
    if (result instanceof Boolean) {
        //handle boolean
    } else if (result instanceof String) {
        //handle String
    }
    findPreference(key.getKey()).setSummary(result);
}

Solution 3:

I had this error too. It happined when i first had a checkbox preference, then used the same name for a list preference, but on my phone in the preferences it was still saved as a boolean and it tried to load that into my list.

fix: clear app data


Solution 4:

I was getting this error too, and couldn't figure out why. I think it's because I added a new preference and tried running the app again. I uninstalled the app, then ran it again with the new preferences, and it worked. Maybe it needed a full wipe to reset the sharedpreferences file.


Post a Comment for "Preference Cannot Cast Java.lang.boolean To String"