private static void bindPreferenceSummaryToValue(Preference preference) { // Set the listener to watch for value changes. preference.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener); // Trigger the listener immediately with the preference's // current value. if (preference instanceof SwitchPreference) { // Added special handling as boolean sBindPreferenceSummaryToValueListener.onPreferenceChange( preference, PreferenceManager.getDefaultSharedPreferences( preference.getContext()).getBoolean(preference.getKey(),true)); } else { sBindPreferenceSummaryToValueListener.onPreferenceChange(preference, PreferenceManager .getDefaultSharedPreferences(preference.getContext()) .getString(preference.getKey(), "")); } }
Programming Tips - Unable to start activity ComponentInfo{.SettingsActivity}: java.lang.ClassCastException: java.lang.Boolean cannot be cast to java.lang.String
Date: 2017mar23
OS: Android
Language: Java
Q. Unable to start activity ComponentInfo{.SettingsActivity}: java.lang.ClassCastException: java.lang.Boolean cannot be cast to java.lang.String
A. I had this because the stock bindPreferenceSummaryToValue() didn't correctly support boolean for SwitchPreference.
The solution was to add a special case for SwitchPreference.