settheline settheline - 5 months ago 279
Android Question

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

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;

Answer

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

Comments