简体   繁体   中英

Android: null object reference when getting selected item value from spinner

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)

You need to pass data from one activity to another. Please refer to How do I pass data between Activities in Android application? .

Make sure you are using the right layout XML that contains Spinner widget with id spinner .

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.your_layout_that_contains_spinner);
    spinner = (Spinner) findViewById(R.id.spinner);

    .............
    ...................... 
}

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?

Yes, this is most likely the problem. findViewById() will only search through the view hierarchy of the current activity. You cannot use it to get a view in another activity. Instead, you must pass data between activities in an Intent. See Start Another Activity for details about how to do this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM