sharedPref.getString() 从 ListPreference 的条目而不是 entryValues 中选取值

sharedPref.getString() picking value from ListPreference's entries instead of entryValues

我的 activity 中有一个 ListPreference:

<ListPreference
        android:key="pref_rValue"
        android:entries="@array/r"
        android:summary="***"
        android:entryValues="@array/rValues"
        android:title="Choose"
        android:defaultValue="2"/>

android:entries="@array/r"android:entryValues="@array/rValues"strings.xml中分别定义如下:

    <string-array name="r">
        <item>0.5 km</item>
        <item>1 km</item>
        <item>2 km</item>
        <item>5 km</item>
        <item>10 km</item>
    </string-array>

    <string-array name="rValues">
        <item>0.5</item>
        <item>1.0</item>
        <item>2.0</item>
        <item>5.0</item>
        <item>10.0</item>
    </string-array>

SettingActivity 中定义的键:public static final String KEY_PREF_SYNC_CONN = "pref_rValue";

下面是我如何使用密钥,然后尝试在 MainActivity 中获取此 ListPreference 的值:

SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
String rValue = sharedPref.getString(SettingsActivity.KEY_PREF_SYNC_CONN, "5");
Toast.makeText(getBaseContext(), "rValue: " + rValue, Toast.LENGTH_SHORT).show();

但是当我尝试将 rValue 字符串用作 Double 时,我收到此错误:java.lang.NumberFormatException: Invalid double: "2 km" 并且上面的 Toast 也显示 "rValue: 2 km".

我想知道为什么不是从 android:entryValues="@array/rValues" 中获取值,而是从 android:entries="@array/r" 中获取值?

请帮我解决这个问题。

这是一个非常简单而愚蠢的问题。

我刚刚卸载了应用程序,重新安装后一切正常。

但是,我很想知道如何确保以后不会再发生类似的事情。