名单preference:使用字符串数组作为输入和整数数组项值不起作用数组、整数、字符串、不起作用

2023-09-05 09:58:32 作者:Welcome to my life

我使用的settings.xml文件列表preference。我想向用户显示的3个选项列表中进行选择。当用户选择在设置中的选项之一,我得到这个错误:

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:

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

这是的名单preference的code:

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>

在我onShared preferenceChanged我以这种方式使用这些值:

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

}

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

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"

那么我不得不改变我的codeA不多,但它的工作原理:

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

为什么我不能用一个字符串数组和一个整数数组列表preference项和值?

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

推荐答案

答案很简单:因为安卓是这样设计的。它只是使用字符串阵列来进行项目和项值,这一切。我看不出有什么问题,在这一点,因为你可以很容易地转换字符串 INT 使用的Integer.parseInt()方法。希望这有助于。

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.