78

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?

4

4 回答 4

91

答案很简单:因为 Android 就是这样设计的。它只是String为条目和条目值使用数组,仅此而已。而且我看不出有任何问题,因为您可以使用该方法轻松地将 a 转换String为a 。希望这可以帮助。intInteger.parseInt()

于 2012-07-05T15:03:55.423 回答
7

您可以将您的条目值转换为字符串以保持ListPreference快乐,然后int在与持久数据存储交谈时将它们转换为 s。

  1. 设置条目值时,使用字符串而不是整数:"1", "2", "3"
  2. 自定义IntListPreference将值保留为整数
  3. 在您的preferences.xml文件中,将 更改<ListPreference><your.app.package.IntListPreference>

IntListPreference.java

这是一个示例实现。在 AndroidX Preference 1.0.0 上测试并运行。

public class IntListPreference extends ListPreference {

    public IntListPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    public IntListPreference(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public IntListPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public IntListPreference(Context context) {
        super(context);
    }

    @Override
    protected boolean persistString(String value) {
        int intValue = Integer.parseInt(value);
        return persistInt(intValue);
    }

    @Override
    protected String getPersistedString(String defaultReturnValue) {
        int intValue;

        if (defaultReturnValue != null) {
            int intDefaultReturnValue = Integer.parseInt(defaultReturnValue);
            intValue = getPersistedInt(intDefaultReturnValue);
        } else {
            // We haven't been given a default return value, but we need to specify one when retrieving the value

            if (getPersistedInt(0) == getPersistedInt(1)) {
                // The default value is being ignored, so we're good to go
                intValue = getPersistedInt(0);
            } else {
                throw new IllegalArgumentException("Cannot get an int without a default return value");
            }
        }

        return Integer.toString(intValue);
    }

}
于 2019-04-27T00:03:50.983 回答
3

答案列在List Preference Documentation中。

int findIndexOfValue (String value)

将返回给定值的索引,并且参数被视为一个字符串,因此 entryValues 数组应该是一个字符串数组来完成这项工作。

于 2016-11-22T15:39:00.473 回答
-5

以下对我有用:

String objectName = prefs.getString("listPrefMelodyYd1", "");
switch (objectName.toUpperCase()) {
    case "1":
        playSound(catSound);
        break;
    case "2":
        playSound(dogSound);
        break;
    case "3":
        playSound(cowSound);
        break;
}
于 2017-12-25T12:12:02.900 回答