I have this spinner where I want to find what the selected item is. This is what I currently have running:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
spinner = (Spinner) findViewById(R.id.spinner);
adapter = ArrayAdapter.createFromResource(this, R.array.teams, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
teamText = parent.getItemAtPosition(pos).toString();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
but for some reason when it gets to that that part, the app crashes and says that the spinner is null:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.brandon.checkpoints/com.example.jit.checkpoints.MapsActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Spinner.setAdapter(android.widget.SpinnerAdapter)' on a null object reference
....
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Spinner.setAdapter(android.widget.SpinnerAdapter)' on a null object reference
Any ideas why this might be happening? I get that the spinner is null, but what I'm trying to figure out is why it's null. May it be because the spinner is in another activity, previous to this one? What I'm trying to do is:
1) user selects route from spinner in activity 1
2) user clicks next, on the next page(activity) the selected value from spinner is put into a variable.
3) The variable is then used to determine which route the user gets (example: if user chose 1 from the spinner, then the user gets route 1, if user chose 2, they get route two etc)