Dave's Brain

Browse - 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.

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(), "")); } }

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.