ListPreference:使用string-array作为Entry和integer-array作为Entry Values不起作用

问题描述 投票:68回答:4

我在settings.xml文件中使用了ListPreference。我想向用户显示3个选项列表。当用户选择“设置”中的某个选项时,出现此错误:

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)

这是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" />

以下是我用来填充条目的数组:

<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>

在我的onSharedPreferenceChanged中,我以这种方式使用这些值:

@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;
        }
}

如果我为entryValues使用另一个字符串数组,它可以工作。例如,如果我使用相同的字符串数组作为值和条目:

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

然后我必须稍微改变我的代码,但它的工作原理:

        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;
        }

为什么我不能对ListPreference条目和值使用字符串数组和整数数组?

android listpreference
4个回答
81
投票

答案很简单:因为Android是这样设计的。它只是对条目和条目值使用String数组,这就是全部。我在这个事实中看不出任何问题,因为你可以使用String方法轻松地将int转换为Integer.parseInt()。希望这可以帮助。


3
投票

答案列在List Preference Documentation中。

int findIndexOfValue (String value)

将返回给定值的索引,并将参数作为String,因此entryValues数组应该是一个字符串数组以使其工作。


1
投票

您可以将条目值转换为字符串以保持ListPreference满意,然后在与持久数据存储进行通信时将它们转换为ints。

  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);
    }

}

-5
投票

以下对我有用:

String objectName = prefs.getString("listPrefMelodyYd1", "");
switch (objectName.toUpperCase()) {
    case "1":
        playSound(catSound);
        break;
    case "2":
        playSound(dogSound);
        break;
    case "3":
        playSound(cowSound);
        break;
}
© www.soinside.com 2019 - 2024. All rights reserved.