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)