android.widget.SearchView cannot be cast to android.support.v7.widget.SearchView-ThrowExceptions

Exception or error:

I’m not unsure of why I’m getting this error. Here’s the menu in question:

<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="com.example.myapp.MainActivity" >

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


<item
    android:id="@+id/action_settings"
    android:orderInCategory="100"
    android:title="@string/action_settings"
    app:showAsAction="never"/>

Here’s the searchable configuration as per the developer guide.

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android" 
            android:label="@string/app_name"
            android:hint="@string/search_hint" >
</searchable>

Added to my manifest file:

<meta-data
                android:name="android.app.searchable"
                android:resource="@xml/searchable" />

I also have an intent handler in the new search activity. Why is this error showing up? My min sdk is 11.

EDIT

In onCreateOptionsMenu:

// Associate searchable config with the SearchView
        SearchManager searchManager = 
                (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView = 
                (SearchView) menu.findItem(R.id.action_search).getActionView();
        searchView.setSearchableInfo(
                searchManager.getSearchableInfo(getComponentName()));
        return true;
How to solve:

you should use these imports instead of using the support library imports

import android.app.SearchManager;
import android.widget.SearchView;
import android.widget.SearchView.OnQueryTextListener;

Just to keep in mind that the minimum SDK is marked as 14

###

In your menu.xml your has to be

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

with that change in the last line

###

Find import android.widget.SearchView; in your imports and replace it with import android.support.v7.widget.SearchView

###

Try adding this:

MenuItem menuItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(menuItem);

It worked for me.

###

instead of using androids

android:actionViewClass="android.widget.SearchView"

you have to use the apps

app:actionViewClass="android.widget.SearchView"

as per the documentation.

###

According to the documentation, In the onCreateOptionsMenu use

MenuItem menuItem = menu.findItem(R.id.action_search);
        SearchView searchView = (SearchView) menuItem.getActionView();

ie:use the menu item directly to call getActionView() method

instead of

MenuItem menuItem = menu.findItem(R.id.action_search);
        SearchView searchView = MenuItemCompat.getActionView(menuItem);

or

searchView = (SearchView) menu.findItem(R.id.action_search);

becouse both are deprecated

###

you can fixed this issue by doing this job in your code

adding this

import android.widget.SearchView;

###

Add this to your build.gradle file

implementation 'com.android.support:appcompat-v7:21.0.3'

and then add this

android.support.v7.widget.SearchView 

instead of

// remove or replace this line 
import android.widget.SearchView;

in your activity file

Leave a Reply

Your email address will not be published. Required fields are marked *