Spinner的getSelectedItem()方法返回NullPointerException

时间:2019-05-13 19:21:57

标签: android spinner

有一系列的微调框,直到它们被触摸,才有适配器。但是,当触摸它们并选择了一个项目时,微调器的getItemSelected()方法将返回空值!

我尝试实现if else语句,该语句检查微调框是否为null,但这不起作用。

if(spinners[i-1][j]!=null){
                        if(spinners[i-1][j].getSelectedItemPosition()==0){ //if nothing selected
                            combined[i][j] = " ";
                        }
                        else{
                            combined[i][j] = spinners[i-1 [j].getSelectedItem().toString();
                        }
                    }
                    else{
                        combined[i][j] = "print Null";
                    }

我只想提取微调器上选择的项目!

1 个答案:

答案 0 :(得分:1)

检查微调器是否已连接适配器。

if(spinners[i-1][j]!=null && spinners[i-1][j].getAdapter()!=null){
    //todo
    }