Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: ListPreference cannot support int values

In my app, I have the values like:

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

and the data:

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

but when I choose one value, the app crashes, there is error:

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

I found this is actually a very old issue raised here. And surprisingly, Google just said it is not a bug and there is no fix? Anyone has suggestions?
This is tested in Android version 4.0.

like image 808
Martin Lau Avatar asked Jan 14 '15 21:01

Martin Lau


2 Answers

Why not use a string-array and convert to integers later (Integer.valueOf()), in code:

<string-array name="font_list_values">
    <item>1</item>
    <item>2</item>
    <item>3</item>
</string-array>
like image 93
Simas Avatar answered Nov 10 '22 08:11

Simas


This bug seems to get old somehow. I also faced this problem. Just use string-array and parse them with Integer.valueOf().

like image 22
Kody Avatar answered Nov 10 '22 07:11

Kody