gpt4 book ai didi

android - 在android微调器上获取空指针

转载 作者:行者123 更新时间:2023-11-30 02:18:17 25 4
gpt4 key购买 nike

我正在尝试将微调器实现到操作栏中,但是当我尝试设置适配器时,我得到了一个空指针引用。我不知道问题出在哪里,因为我之前在 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)

最佳答案

根据其他一些 stackoverflow 帖子,您需要在调用 getActionView 之前对菜单对象调用 setActionView。

这篇文章有以下评论:

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

这里是说明您必须使用 SetActionView 的帖子的链接。

getActionView() of my MenuItem return null

关于android - 在android微调器上获取空指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28934685/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com