Я пытаюсь загрузить свои настройки и обновить счетчик, чтобы показать ранее сохраненное значение.Я никогда раньше не использовал spinner.setSelection и продолжаю получать nullpointerexceptions.
String[] timeItems = new String[] {"After 2 minutes", "After 5 minutes", "Never"};
ArrayAdapter<String> adapter;
Spinner screenSpinner;
onCreate ():
Spinner screenSpinner = (Spinner)findViewById(R.id.spinner_screen);
screenSpinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, timeItems);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
screenSpinner.setAdapter(adapter);
loadPrefs ()
//load the preferences
...
//then update the selection in the spinner
if (mScreenTimer == SCREEN_TIMER_2MINUTES) {
int tmp_index = adapter.getPosition("After 2 minutes");
screenSpinner.setSelection(tmp_index,true);
} else if (mScreenTimer == SCREEN_TIMER_5MINUTES) {
int tmp_index = adapter.getPosition("After 5 minutes");
screenSpinner.setSelection(tmp_index,true);
} else if (mScreenTimer == SCREEN_TIMER_NEVER) {
int tmp_index = adapter.getPosition("Never");
screenSpinner.setSelection(tmp_index,true);
}
Я получаюNullPointerExecption на screenSpinner.setSelection (tmp_index, true)