PearsonArtPhoto PearsonArtPhoto - 5 months ago 12
Android Question

Default bindPreferenceSummaryToValue crashes for non-string types

I'm following the example method to add a compatible preference/ fragment dialog found here. When doing so, I have found that if I have preferences that are Integers, Boolean, etc, it just crashes.

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.
sBindPreferenceSummaryToValueListener.onPreferenceChange(
preference,
PreferenceManager.getDefaultSharedPreferences(
preference.getContext()).getString(preference.getKey(),""));
}


I found that I can make this work for an Integer by changing the
getString()
to
getInteger()
, and using a different bindPreferenceSummaryToValue function depending on what the type is. Of course, this seems like a really inelegant solution, but I'm struggling to figure out what else I can do. Here's the stack trace, BTW.

11-22 19:52:10.068: E/AndroidRuntime(17564): FATAL EXCEPTION: main
11-22 19:52:10.068: E/AndroidRuntime(17564): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.kd7uiy.hamfinder/com.kd7uiy.hamfinder.MainSettingsActivity}: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
11-22 19:52:10.068: E/AndroidRuntime(17564): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
11-22 19:52:10.068: E/AndroidRuntime(17564): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
11-22 19:52:10.068: E/AndroidRuntime(17564): at android.app.ActivityThread.access$600(ActivityThread.java:141)
11-22 19:52:10.068: E/AndroidRuntime(17564): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
11-22 19:52:10.068: E/AndroidRuntime(17564): at android.os.Handler.dispatchMessage(Handler.java:99)
11-22 19:52:10.068: E/AndroidRuntime(17564): at android.os.Looper.loop(Looper.java:137)
11-22 19:52:10.068: E/AndroidRuntime(17564): at android.app.ActivityThread.main(ActivityThread.java:5103)
11-22 19:52:10.068: E/AndroidRuntime(17564): at java.lang.reflect.Method.invokeNative(Native Method)
11-22 19:52:10.068: E/AndroidRuntime(17564): at java.lang.reflect.Method.invoke(Method.java:525)
11-22 19:52:10.068: E/AndroidRuntime(17564): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-22 19:52:10.068: E/AndroidRuntime(17564): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-22 19:52:10.068: E/AndroidRuntime(17564): at dalvik.system.NativeStart.main(Native Method)
11-22 19:52:10.068: E/AndroidRuntime(17564): Caused by: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
11-22 19:52:10.068: E/AndroidRuntime(17564): at android.app.SharedPreferencesImpl.getString(SharedPreferencesImpl.java:224)
11-22 19:52:10.068: E/AndroidRuntime(17564): at com.kd7uiy.hamfinder.MainSettingsActivity.bindPreferenceSummaryToValue(MainSettingsActivity.java:194)

Answer

There isn't a clean solution that I can find, but the best workaround seems to be something like this, using instanceof to figure out what type of data you are dealing with.

private static void bindPreferenceSummaryToValue(Preference preference) {
    // Set the listener to watch for value changes.
    preference
            .setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener);


    if (preference instanceof SeekBarPreference)
    {
        // Trigger the listener immediately with the preference's
        // current value.
        sBindPreferenceSummaryToValueListener.onPreferenceChange(
                preference,
                PreferenceManager.getDefaultSharedPreferences(
                        preference.getContext()).getInt(preference.getKey(),0));
    }
    else if (preference instanceof CheckBoxPreference)
    {
        // Trigger the listener immediately with the preference's
        // current value.
        sBindPreferenceSummaryToValueListener.onPreferenceChange(
                preference,
                PreferenceManager.getDefaultSharedPreferences(
                        preference.getContext()).getBoolean(preference.getKey(),true));
    }
}