在 android 微调器上获取空指针
Getting a null pointer on android spinner
我正在尝试将微调器实现到操作栏中,但是当我尝试设置适配器时,我得到了一个空指针引用。我不知道问题出在哪里,因为我之前在 activity 中设置它时它工作正常。
这是我的代码
@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);
}
这是错误
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)
根据其他一些 Whosebug posts,您需要在调用 getActionView 之前对菜单对象调用 setActionView。
此 post 有以下评论:
Casting a Menu Item in XML to a Spinner (Actionbar appcompat)
No, but it lead me to another question with almost the same name:
whosebug.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
这里是 link 到 post 表示您必须使用 SetActionView。
getActionView() of my MenuItem return null
我遇到了同样的问题。如果您遵循上面的评论链,您会发现您不需要使用 setActionView。您只需要正确设置布局 XML:
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
对我有用。
我正在尝试将微调器实现到操作栏中,但是当我尝试设置适配器时,我得到了一个空指针引用。我不知道问题出在哪里,因为我之前在 activity 中设置它时它工作正常。
这是我的代码
@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);
}
这是错误
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)
根据其他一些 Whosebug posts,您需要在调用 getActionView 之前对菜单对象调用 setActionView。
此 post 有以下评论:
Casting a Menu Item in XML to a Spinner (Actionbar appcompat)
No, but it lead me to another question with almost the same name: whosebug.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
这里是 link 到 post 表示您必须使用 SetActionView。
getActionView() of my MenuItem return null
我遇到了同样的问题。如果您遵循上面的评论链,您会发现您不需要使用 setActionView。您只需要正确设置布局 XML:
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
对我有用。