Getting class cast exception while using android.support.v7.widget.SearchView
Asked Answered
R

1

6

I am using the menu to show the Search icon over the Action Bar. I have create the search view

itemSearch = menu.findItem(R.id.action_search_chat_home_container);
searchView = (SearchView) MenuItemCompat.getActionView(itemSearch);

I am using the android-support-v7-appcompat as library project .

XML for Menu...

<?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>

I am getting the following error

*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)

Code import packages in java file

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;
Radiotelegram answered 10/3, 2014 at 7:15 Comment(0)
M
0

Can you please try the following lines of code?

itemSearch = menu.findItem(R.id.action_search_chat_home_container);
searchView = (SearchView) MenuItemCompat.getActionView(itemSearch).getWrappedView();

Source link

line 414.

Mcevoy answered 8/5, 2014 at 13:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.