我试图在微调器上使用这个适配器,它给出了空指针异常, 请给予你值得回应
ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item, R.array.select_action_arrays);
和我的string.xml文件
<string-array name="select_action_arrays">
<item>alert1</item>
<item>alert2</item>
<item>alert3</item>
<item>alert4</item>
<item>alert5</item>
</string-array>
这是完整的代码行
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.select_action_arrays));
spinnerSelectAction.setAdapter(adapter);
spinnerSelectAction = (Spinner)findViewById(R.id.spinnerSelectAction);
spinnerSelectAction.setAdapter(adapter);
spinnerSelectAction.setOnItemSelectedListener(new OnItemSelectedListener() {
int count=0;
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
if(count >= 1){
Toast.makeText(getBaseContext(),"inside on item selected ",Toast.LENGTH_SHORT).show();
}
count++;
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {}
});
spinnerSelectAction似乎已初始化
答案 0 :(得分:2)
您似乎没有正确初始化适配器。它应该是这样的:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item,
getResources().getStringArray(R.array.select_action_arrays));
答案 1 :(得分:1)
写下面的代码行
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.select_action_arrays));
而不是
ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item, R.array.select_action_arrays);
可能会解决您的问题。
答案 2 :(得分:0)
在从视图中检索微调器之前添加到适配器,第一条指令应该是这样的:
spinnerSelectAction = (Spinner)findViewById(R.id.spinnerSelectAction);
spinnerSelectAction.setAdapter(adapter);