gpt4 book ai didi

android - spinner.setSelection 帮助

转载 作者:行者123 更新时间:2023-11-30 04:39:06 27 4
gpt4 key购买 nike

我正在尝试加载我的首选项并更新微调器以显示之前保存的值。我以前从未使用过 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);

加载参数()

    //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);
}

我在 screenSpinner.setSelection(tmp_index, true) 上得到了 NullPointerExecption

最佳答案

设置适配器后尝试设置选择

关于android - spinner.setSelection 帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6318648/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com