开发者

Transforming a Locale[] to a CharSequence[] to place inside a ListPreference in Android App

开发者 https://www.devze.com 2023-04-12 16:57 出处:网络
In my Android app, I want to place a list of all the available Locales on the phone into a ListPreference in my PreferenceActivity so that the user can select which one he wants to change the output o

In my Android app, I want to place a list of all the available Locales on the phone into a ListPreference in my PreferenceActivity so that the user can select which one he wants to change the output of the currency symbol.

So far I've tried this:

addPreferencesFromResource(R.xml.preferences);
PreferenceScreen root = getPreferenc开发者_如何学JAVAeManager().createPreferenceScreen(this);

ListPreference listPref = (ListPreference) root.findPreference("list");
Locale[] availableLocales = Locale.getAvailableLocales();

CharSequence[] entries = new CharSequence[Locale.getAvailableLocales().length];
CharSequence[] entriesVal = new CharSequence[Locale.getAvailableLocales().length];

for (int i = 0; i < entriesVal.length; i++) {
        entries[i] = availableLocales[i].getDisplayCountry();
        entriesVal[i] = Integer.toString(i);
}

listPref.setEntries(entries);
listPref.setEntryValues(entriesVal);

I get a JavaNullPointer Exception on this line listPref.setEntries(entries);, so I believe that I haven't formed my CharSequence properly, so the ListPReference cannot read it.

any ideas?


It seems more likely that listPref is actually null - i.e. root.findPreference("list") is returning null. That's what you should look into (and at least check) first.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号