Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)
like image 328
PearsonArtPhoto Avatar asked Nov 23 '13 01:11

PearsonArtPhoto


2 Answers

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));
    }
}
like image 52
PearsonArtPhoto Avatar answered Nov 07 '22 10:11

PearsonArtPhoto


private static void bindPreferenceSummaryToValue(Preference preference) {
//Todo
}

this method is used for showing the current value of your preference, e.g If you'r using <SwitchPreference/> which has value as true or false it will display that value in summary. You do not need to show boolean values as summary. This method is used for <ListPreference/> or <RingtonePreference/> where you would like to see current value. Summary in this case is string but for <SwitchPreference/> it's boolean. Either you can ignore these boolean summary or can using the instanceof you can avoid exception.

like image 38
ketankk Avatar answered Nov 07 '22 10:11

ketankk