我在尝试访问spn_make上的getSelectedItem()。toString()时遇到NULL指针异常。
CustomAdapter adapter = new CustomAdapter(InfoActivity.this);
spn_make.setAdapter(adapter);
Log.i("Count", spn_make.getAdapter().getCount()+"");
spn_make.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> a, View v,
int pos, long arg3) {
Toast.makeText(InfoActivity.this, spn_make.getSelectedItem().toString(), Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> a) {
// TODO Auto-generated method stub
}
});
请允许任何人帮助我。
答案 0 :(得分:2)
要检索您必须编写的所选项目
a.getItemAtPosition(pos)
而不是spn_make.getSelectedItem()
Here是供参考的文档
更改此行代码
Toast.makeText(InfoActivity.this, spn_make.getSelectedItem().toString(), Toast.LENGTH_LONG).show();
到此:
Toast.makeText(InfoActivity.this, a.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
答案 1 :(得分:0)
if (Spinner.getSelectedItem() == null) {
return;
}else {
Textview tv = Spinner.getSelectedItem().toString();
}