开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜