The question is about android Spinners working when “Gone” and then “Visible” again.
I’ve got a form where spinner is hidden (“Gone”) by default. If user checks checkbox, it appears and allows to choose something. The problem is when I do submit info from the form without touching the checkbox (i.e. default value is chosen from spinner), App crashes with nullpointer exception – despite I`m setting spinner as visible.
If I click checkbox (even twice, to make Spinner gone again) – everything works fine. If I use invisible instead of gone, everything works fine even without touching the button.
How to make it work with spinner “Gone” by default?
Here`s code on how checkbox behaves (onclick checkbox):
case R.id.checkboxUseDefaultURLList:
{
final CheckBox checkBoxDefaultList = (CheckBox) findViewById(R.id.checkboxUseDefaultURLList);
final TextView textViewEmpty = (TextView) findViewById(R.id.textBlank);
final Spinner s2 = (Spinner) findViewById(R.id.spinnerURLList);
if (checkBoxDefaultList.isChecked()) {
textViewEmpty.setVisibility(View.GONE); // GONE
textViewEmpty.invalidate();
s2.setVisibility(View.GONE); // GONE
s2.invalidate();
}
if (!checkBoxDefaultList.isChecked()) {
textViewEmpty.setVisibility(View.VISIBLE); // VISIBLE
textViewEmpty.invalidate();
s2.setVisibility(View.VISIBLE); // VISIBLE
s2.invalidate();
}
break;
}
here`s how I get values from Spinner (in onclick of “submit” button):
Spinner spinURLList = (Spinner) findViewById(R.id.spinnerURLList);
spinURLList.setVisibility(View.VISIBLE);
spinURLList.invalidate();
spinURLList.setSelection(0);
TextView chosenURLTV = (TextView) spinURLList.getSelectedView();
String urlListSelected = chosenURLTV.getText().toString();
Any ideas why it crashes if I don`t click on checkbox before submitting?
It may be a problem of assigning the
TextViewthe text in the spinner. Try: