我正在使用菜单在动作栏上显示搜索图标。我已经创建了搜索视图
itemSearch = menu.findItem(R.id.action_search_chat_home_container);
searchView = (SearchView) MenuItemCompat.getActionView(itemSearch);
我将Android-Support-V7-AppCompat用作库项目。
菜单的XML ...
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:homecontainer="http://schemas.android.com/apk/res-auto" >
<item
android:id="@+id/action_search_chat_home_container"
android:icon="@drawable/ic_action_search"
android:title="@string/action_search"
homecontainer:showAsAction="ifRoom|collapseActionView"
homecontainer:actionViewClass="android.support.v7.widget.SearchView"/>
</menu>
我收到以下错误
*03-18 12:19:46.965 E/com.abc.contactbook.RTContactBookActivity<======(14993): java.lang.ClassCastException: android.support.v7.internal.view.menu.MenuItemWrapperICS$CollapsibleActionViewWrapper cannot be cast to android.support.v7.widget.SearchView
03-18 12:19:46.965 E/com.abc.contactbook.RTContactBookActivity<======(14993): at com.abc.contactbook.RTContactBookActivity.onCreateOptionsMenu(RTContactBookActivity.java:874)
03-18 12:19:46.965 E/com.abc.contactbook.RTContactBookActivity<======(14993): at android.support.v4.app.Fragment.performCreateOptionsMenu(Fragment.java:1560)
03-18 12:19:46.965 E/com.abc.contactbook.RTContactBookActivity<======(14993): at android.support.v4.app.FragmentManagerImpl.dispatchCreateOptionsMenu(FragmentManager.java:1949)
03-18 12:19:46.965 E/com.abc.contactbook.RTContactBookActivity<======(14993): at android.support.v4.app.Fragment.performCreateOptionsMenu(Fragment.java:1563)
03-18 12:19:46.965 E/com.rancore.contactbook.RTContactBookActivity<======(14993): at android.support.v4.app.FragmentManagerImpl.dispatchCreateOptionsMenu(FragmentManager.java:1949)
03-18 12:19:46.965 E/com.abc.contactbook.RTContactBookActivity<======(14993): at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:225)
03-18 12:19:46.965 E/com.abc.contactbook.RTContactBookActivity<======(14993): at android.support.v7.app.ActionBarActivity.superOnCreatePanelMenu(ActionBarActivity.java:224)
03-18 12:19:46.965 E/com.abc.contactbook.RTContactBookActivity<======(14993): at android.support.v7.app.ActionBarActivityDelegateICS.onCreatePanelMenu(ActionBarActivityDelegateICS.java:141)
03-18 12:19:46.965 E/com.abc.contactbook.RTContactBookActivity<======(14993): at android.support.v7.app.ActionBarActivity.onCreatePanelMenu(ActionBarActivity.java:199)
03-18 12:19:46.965 E/com.abc.contactbook.RTContactBookActivity<======(14993): at android.support.v7.app.ActionBarActivityDelegateICS$WindowCallbackWrapper.onCreatePanelMenu(ActionBarActivityDelegateICS.java:280)
03-18 12:19:46.965 E/com.abc.contactbook.RTContactBookActivity<======(14993): at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:455)
Java文件中的代码导入软件包
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.SearchView.OnQueryTextListener;
您可以尝试以下代码行吗?
itemSearch = menu.findItem(R.id.action_search_chat_home_container);
searchView = (SearchView) MenuItemCompat.getActionView(itemSearch).getWrappedView();
源链接
第414行。