Preference cannot cast java.lang.boolean to String

后端 未结 4 1750
名媛妹妹
名媛妹妹 2021-01-12 17:02

I have the following Preference class:

public class AppPreferencesActivity extends PreferenceActivity {
    private SharedPreferences appPrefs;
    private S         


        
相关标签:
4条回答
  • 2021-01-12 17:17

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

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-12 17:33

    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

    0 讨论(0)
  • 2021-01-12 17:39

    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);
    }
    
    0 讨论(0)
提交回复
热议问题