Android: ListPreference cannot support int values
Asked Answered
V

2

6

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.

Vortex answered 14/1, 2015 at 21:50 Comment(0)
W
8

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>
Washer answered 14/1, 2015 at 21:55 Comment(0)
H
2

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

Halland answered 14/1, 2015 at 21:56 Comment(1)
thanks Kody, yeah, actually I did use String array to work around this, but it is strange that google didn't even look at this issue.Vortex

© 2022 - 2024 — McMap. All rights reserved.