Checking if spinner is selected and having null value in Android

后端 未结 6 830
庸人自扰
庸人自扰 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

    spinnerName is null or if getSelectedItem() returns null, calling toString() will cause your app to crash for NPE

    String name= null;
    if(spinnerName != null && spinnerName.getSelectedItem() !=null ) {
       name = (String)spinnerName.getSelectedItem();
    } else  { 
    
    }
    

提交回复
热议问题