Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

android

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;
like image 454
settheline Avatar asked Jul 02 '14 04:07

settheline


4 Answers

In the menu.xml your <item></item> 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

like image 133
DejaVu Avatar answered Nov 01 '22 08:11

DejaVu


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

like image 43
Anuj Avatar answered Nov 01 '22 09:11

Anuj


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

like image 17
ሹሉቃ ሻምባ Avatar answered Nov 01 '22 08:11

ሹሉቃ ሻምባ


Try adding this:

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

It worked for me.

like image 3
user3223097 Avatar answered Nov 01 '22 09:11

user3223097