-1

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)

jit
  • 1
  • 5
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Chris Stillwell May 17 '17 at 20:19
  • @ChrisStillwell 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 where this code is executed? – jit May 17 '17 at 20:26
  • It's null because you aren't inflating your layout before calling findViewById. – Chris Stillwell May 17 '17 at 20:31
  • @ChrisStillwell even when this wasn't in onCreate and was in a button click it still didn't work. Inflating layout doesn't seem to do anything. In the full length code the layout inflation was in, before calling findViewById. – jit May 17 '17 at 20:42
  • 1
    Can you post your xml layout file then? You still need to inflate your layout for your Activity or Fragment. If you don't inflate it, `findViewById` will return null regardless of the value you give it. – Chris Stillwell May 17 '17 at 20:46
  • Please post a [mcve]. You should show the exact code which performs steps 1-3. Note, I am **not** asking you to post your entire app. Just the portions of code which are important for understanding your question. Note also, that the code should **compile**. This means that you need to include all methods inside a class, just like you would in a .java file. – Code-Apprentice May 17 '17 at 21:02
  • @ChrisStillwell https://pastebin.com/bNWr9jDL it is telling me that the map fragment is unknown but it ran perfectly before the addition of getting the spinner value. Just a reminder that the spinner is on a previous activity, not this one. I already tried bundling the value in previous activity (which works) and sending it with intent, but when calling the bundled extra in the next activity (the one where I need the value), the app crashes again. – jit May 17 '17 at 21:03
  • @jit there is no Spinner with the id `spinner` in your layout file. – Chris Stillwell May 17 '17 at 21:06
  • @ChrisStillwell thats what I have been telling the whole time, the spinner is in the previous activity, I'm trying to get the spinner value from the previous activity/layout. – jit May 17 '17 at 21:08
  • if you want to pass data from 1 activity to another. check this http://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android-application – Saurabh May 17 '17 at 21:11
  • @Saurabh this works, how can I choose this as the best answer? – jit May 17 '17 at 21:21

3 Answers3

1

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

Community
  • 1
  • 1
Saurabh
  • 1,225
  • 9
  • 16
  • setContent is in the full length code, before the findViewById. May it be because the spinner is in another activity, previous to this one where this code is executed? – jit May 17 '17 at 20:45
  • can you post your XML you're passing on setcontentview? – Saurabh May 17 '17 at 20:48
  • there is no spinner in the xml, obviously it will be null. findViewById searches the XML for the view which is not there – Saurabh May 17 '17 at 21:10
1

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);

    .............
    ...................... 
}
Ferdous Ahamed
  • 21,438
  • 5
  • 52
  • 61
0

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.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268