Checking if spinner is selected and having null value in Android

后端 未结 6 798
庸人自扰
庸人自扰 2021-01-12 09:27

I want to check first if spinner has null values based on the following:

String Name= spinnerName.getSelectedItem().toString();
if(Name != null) {     
} els         


        
6条回答
  •  南笙
    南笙 (楼主)
    2021-01-12 10:20

    Do not add toString(). If you add toString() it tries to convert null to String, then it throws Exception.

    if(spinnerName.getSelectedItem() !=null ) {
       name = spinnerName.getSelectedItem().toString();
    }
    

提交回复
热议问题