gpt4 book ai didi

android - menu.findItem 返回 null

转载 作者:可可西里 更新时间:2023-11-01 19:08:59 25 4
gpt4 key购买 nike

我找不到解决方案请帮忙

工具:

public class VentasActivity extends ActionBarActivity
implements NavigationDrawerFragment.NavigationDrawerCallbacks, android.support.v7.widget.SearchView.OnQueryTextListener

哪里返回空:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
if (!mNavigationDrawerFragment.isDrawerOpen()) {
getMenuInflater().inflate(R.menu.sales, menu);
restoreActionBar();

// HERE RETURNS NULL
searchView = (SearchView) menu.findItem(R.id.action_product_search).getActionView();

if (searchView != null) {
final Menu menu_block = menu;
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

@Override
public boolean onQueryTextSubmit(String query) {
// collapse the view ?
menu_block.findItem(R.id.action_product_search).collapseActionView();
return false;
}

@Override
public boolean onQueryTextChange(String newText) {
// search goes here !!
// listAdapter.getFilter().filter(query);
return false;
}
});
Log.i("sales module", "SearchView OK");
} else
Log.i("sales module", "SearchView is null");


return true;
}
return super.onCreateOptionsMenu(menu);
}

和 sales.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >

<item android:id="@+id/action_product_search"
android:title="@string/action_search"
android:icon="@drawable/ic_action_search"
app:showAsAction="always|collapseActionView"
android:actionViewClass="android.widget.SearchView" />

非常非常感谢!

更新:restoreActionBar() 的代码

public void restoreActionBar() {
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setTitle(mTitle);
}

最佳答案

终于找到问题了:

我将/res/menu/sales.xml 中的最后一行更改为:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<!-- Search Widget -->
<item android:id="@+id/search_view_productos"
android:title="@string/action_search"
android:icon="@drawable/ic_action_search"
app:showAsAction="always|collapseActionView"
android:actionViewClass="android.widget.SearchView" />
</menu>

为此:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<!-- Search Widget -->
<item android:id="@+id/search_view_productos"
android:title="@string/action_search"
android:icon="@drawable/ic_action_search"
app:showAsAction="always|collapseActionView"
app:actionViewClass="android.widget.SearchView" />
</menu>

问题是 actionViewClass 上的 xml 命名空间

关于android - menu.findItem 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22033323/

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