ListPreference: use string-array as Entry and inte

2019-01-22 10:49发布

问题:

I'm using in a settings.xml file a ListPreference. I want to show the user a list of 3 options to select. When the user chooses one of the options in the Settings, I get this error:

java.lang.NullPointerException
    at android.preference.ListPreference.onDialogClosed(ListPreference.java:264)
    at android.preference.DialogPreference.onDismiss(DialogPreference.java:381)
    at android.app.Dialog$ListenersHandler.handleMessage(Dialog.java:1228)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4424)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
    at dalvik.system.NativeStart.main(Native Method)

This is the code of the ListPreference:

<ListPreference
    android:entries="@array/date_alignement"
    android:entryValues="@array/date_alignement_values"
    android:key="settings_date_alignement"
    android:summary="@string/settings_date_alignement_summary"
    android:title="@string/settings_date_alignement_title" />

And here are the arrays I use to populate the entries:

<string-array name="date_alignement">
    <item>"Top"</item>
    <item>"Center"</item>
    <item>"Bottom"</item>
</string-array>
<integer-array name="date_alignement_values">
    <item >0</item>
    <item >1</item>
    <item >2</item>
</integer-array>

In my onSharedPreferenceChanged I use those values in this way:

@Override
public void onSharedPreferenceChanged(
            SharedPreferences sharedPreferences, String key) {          

        //Text
        mAlignment =  mPrefs.getInt(PREF_ALIGNMENT, 1);
        switch (mAlignment) {
        case 0:
            offsetY = mHeight/3.0f;
            break;

        case 2:
            offsetY = mHeight*2.0f/3.0f;
            break;

        default:
            offsetY = mHeight/2.0f;
            break;
        }
}

If I use another string-array for the entryValues it works. For example if I use the same string array as values and entries:

    android:entries="@array/date_alignement"
    android:entryValues="@array/date_alignement"

then I have to change my code a little but it works:

        if(mAlignment.equalsIgnoreCase("center")) {
            offsetY = mHeight/2.0f;
        } else if(mAlignment.equalsIgnoreCase("top")) {
            offsetY = mHeight/3.0f;
        } else if(mAlignment.equalsIgnoreCase("bottom")) {
            offsetY = mHeight*2.0f/3.0f;
        }

Why I can't use a string-array and an integer-array for a ListPreference entries and values?

回答1:

The answer is simple: because the Android is designed this way. It just uses String arrays for both entries and entry values and that's all. And I can't see any problem in this fact, since you can easily convert a String to an int using the Integer.parseInt() method. Hope this helps.



回答2:

The answer is listed in List Preference Documentation.

int findIndexOfValue (String value)

will return the index for given value and the argument is taken as a String, so the entryValues array should be a string array to get this work.



回答3:

The following worked for me:

String objectName = prefs.getString("listPrefMelodyYd1", "");
switch (objectName.toUpperCase()) {
    case "1":
        playSound(catSound);
        break;
    case "2":
        playSound(dogSound);
        break;
    case "3":
        playSound(cowSound);
        break;
}