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.
精彩评论