Martin Lau
Martin Lau

Reputation: 219

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.

Upvotes: 6

Views: 2727

Answers (2)

Kody
Kody

Reputation: 1254

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

Upvotes: 2

Simas
Simas

Reputation: 44178

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>

Upvotes: 8

Related Questions