其他分享
首页 > 其他分享> > Android:ListPreference不支持int值

Android:ListPreference不支持int值

作者:互联网

在我的应用程序中,我有以下值:

<ListPreference
    android:key="font_setting"
    android:title="@string/font"
    android:summary="%s"
    android:entries="@array/font_list"
    android:entryValues="@array/font_list_values"
    android:dialogTitle="@string/choose_font" />

和数据:

<string-array name="font_list">
    <item>"Small"</item>
    <item>"Medium"</item>
    <item>"Big"</item>
</string-array>
<integer-array name="font_list_values">
    <item>1</item>
    <item>2</item>
    <item>3</item>
</integer-array>

但是当我选择一个值时,应用程序崩溃,就会出现错误:

java.lang.NullPointerException
at android.preference.ListPreference.onDialogClosed(ListPreference.java:264)

我发现这实际上是一个非常古老的问题here.而且令人惊讶的是,谷歌只是说它不是一个bug而且没有修复?有人有建议吗?
这是在Android 4.0版中测试的.

解决方法:

为什么不使用字符串数组并在以后转换为整数(Integer.valueOf()),在代码中:

<string-array name="font_list_values">
    <item>1</item>
    <item>2</item>
    <item>3</item>
</string-array>

标签:android,listpreference
来源: https://codeday.me/bug/20190612/1223674.html