sousheel
sousheel

Reputation: 2489

Getting a null pointer on android spinner

I'm trying to implement the spinner into the action bar, however I'm getting a null pointer reference when I try to set the adapter. I don't know what the problem is because it was working fine when I set it previously in the activity.

This is my code

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    if (!mNavigationDrawerFragment.isDrawerOpen()) {
        // Inflate the menu; this adds items to the action bar if it is present.if (!mNavigationDrawerFragment.isDrawerOpen()) {
        // Only show items in the action bar relevant to this screen
        // if the drawer is not showing. Otherwise, let the drawer
        // decide what to show in the action bar.
        getMenuInflater().inflate(R.menu.menu_game, menu);
        restoreActionBar();
        Spinner s = (Spinner) menu.findItem(R.id.menu_spinner).getActionView(); // find the spinner
        /*SpinnerAdapter mSpinnerAdapter = ArrayAdapter.createFromResource(this.getSupportActionBar()
                .getThemedContext(), R.array.mile_selections, android.R.layout.simple_spinner_dropdown_item); //  create the adapter from a StringArray*/
        ArrayAdapter<CharSequence> spinnerAdapter = ArrayAdapter.createFromResource(this, R.array.mile_selections, android.R.layout.simple_spinner_item);
        spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        s.setAdapter(spinnerAdapter); // set the adapter
        s.setOnItemSelectedListener(this);
        return true;
    }
    return super.onCreateOptionsMenu(menu);
}

This is the error

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Spinner.setAdapter(android.widget.SpinnerAdapter)' on a null object reference
        at com.sousheelvunnam.scrimmage.ui.GameActivity.onCreateOptionsMenu(GameActivity.java:138)

Upvotes: 1

Views: 5213

Answers (2)

Mallak
Mallak

Reputation: 16

I was having this same problem. If you follow the comment chains above, you find out that you don't need to use setActionView. You just need to set your layout XML properly:

I had the same issue and I my error was that I wasn't properly using the actionLayout xml attribute properly. First of all, you must define a custom namespace in the menu tag like this xmlns:app="http://schemas.android.com/apk/res-auto" Then on the menu item you want to have to use the actionLayout attribute like this: app:actionLayout="@layout/month_picker" In this case the month_picker layout is a spinner. Doing this makes it unnecessary to use the setActionView method. – Jorge Salas May 25 '15 at 0:50

Worked for me.

Upvotes: 0

Reenactor Rob
Reenactor Rob

Reputation: 1526

According to some other stackoverflow posts, you need to call setActionView on the menu object before calling getActionView.

This post has the comment below:

Casting a Menu Item in XML to a Spinner (Actionbar appcompat)

No, but it lead me to another question with almost the same name: stackoverflow.com/questions/14286768/… Apperently getActionView doesnt work unless you use setActionView first. So it does not pick it up from XML – ThomQ Apr 10 '14 at 0:01

And here is the link to the post that says you have to use SetActionView.

getActionView() of my MenuItem return null

Upvotes: 1

Related Questions