Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android MultiSelectListPreference, java.lang.String cannot be cast to java.util.Set

I try to implement MultiSelectListPreference for my settings view:

<MultiSelectListPreference
    android:title="@string/title"
    android:summary="@string/message"
    android:key="pref_foobar"
    android:defaultValue="@array/empty_array"
    android:entries="@array/fooArray"
    android:entryValues="@array/fooValues" />

The array:

<resources>
    <string-array name="fooArray">
        <item>Foo</item>
        <item>Bar</item>
    </string-array>
    <string-array name="fooValues">
        <item>foo_value</item>
        <item>bar_value</item>
    </string-array>
    <string-array name="empty_array"/>
</resources>

However this gives me following exception:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myproject/.SettingsActivity}: java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Set

Full stacktrace: http://pastebin.com/kBDByXY5

Any ideas whats wrong?


Code:

public class SettingsActivity extends Activity {

    private static final String TAG_SETTINGS_FRAGMENT = "SETTINGS_FRAGMENT";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                                .replace(android.R.id.content, new PreferenceFragment(), TAG_SETTINGS_FRAGMENT).commit();
        }
    }

    public static class PreferenceFragment extends android.preference.PreferenceFragment{

        public PreferenceFragment() {
            //required
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.preferences);
        }
    }
}
like image 918
UpCat Avatar asked Oct 03 '14 12:10

UpCat


1 Answers

It feels like you already have a plain String preference under this key on your device or emulator. Try completely uninstalling and reinstalling the app, or using "Clear Data" for the app in its screen in Settings, to wipe out your existing SharedPreferences.

like image 109
CommonsWare Avatar answered Nov 10 '22 22:11

CommonsWare