Android: problems with getSelectedItem on a spinner

后端 未结 6 1570
旧时难觅i
旧时难觅i 2021-01-27 00:35

I have a Spinner, and put the selected item in the body of a mail. this is my code:

@Override
protected void onCreate(Bundle savedInstanceState) {
         


        
6条回答
  •  长发绾君心
    2021-01-27 00:57

    getSelectedItem() returns null if there is nothing selected on your spinner and calling toString() is making your application crash. Get rid of

    final String taglia = spinnerTaglia.getSelectedItem().toString();
    

    and in your onClick do:

    if (spinnerTaglia.getSelectedItem() == null) {
          return;
    }
    String taglia = spinnerTaglia.getSelectedItem().toString();
    // the other code
    

提交回复
热议问题