我将SherlockActionBar
用于我的ActionBar
,但我想将AppCompact用于我的搜索视图。要将我的代码从sherlock更改为AppCompact,我写了这个:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
// SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.action_search));
//searchView.setOnQueryTextListener(this);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
return false;
}
@Override
public boolean onQueryTextChange(String s) {
return false;
}
});
return true;
// return super.onCreateOptionsMenu(menu);
}
xml菜单:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item
android:id="@+id/action_search"
android:actionViewClass="android.support.v7.widget.SearchView"
android:icon="@android:drawable/ic_menu_search"
android:title="action_search"
app:showAsAction="always|collapseActionView" />
</menu>
但它崩溃了。我该如何解决?
修改 更改 android:actionViewClass到app:actionViewClass