Reputation: 3383
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;
Upvotes: 20
Views: 54131
Reputation: 915
<item
android:id="@+id/app_bar_search"
android:icon="@drawable/ic_search_black_24dp"
android:title="@string/search"
app:showAsAction="ifRoom|withText"
app:actionViewClass="androidx.appcompat.widget.SearchView"/>
import androidx.appcompat.widget.SearchView
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.search_menu, menu)
val search = menu.findItem(R.id.app_bar_search)
val searchView = search.actionView as SearchView
searchView.queryHint = "Search"
searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query: String?): Boolean {
return false
}
override fun onQueryTextChange(newText: String?): Boolean {
adapter.filter.filter(newText)
return true
}
})
return super.onCreateOptionsMenu(menu)
}
Upvotes: 0
Reputation: 41
This answer is for those using androidx
add this to build.gradle.
implementation 'androidx.appcompat:appcompat:1.3.1'
then add this
app:actionViewClass="androidx.appcompat.widget.SearchView"
and import this in the ManiActivity.kt
import androidx.appcompat.widget.SearchView
Upvotes: 2
Reputation: 603
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
Upvotes: 46
Reputation: 61
For me i have only changed
app:actionViewClass="android.widget.SearchView"
Insted of
app:actionViewClass="androidx.appcompat.widget.SearchView"
Upvotes: 6
Reputation: 494
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
Upvotes: 0
Reputation: 85
instead of using androids
android:actionViewClass="android.widget.SearchView"
you have to use the apps
app:actionViewClass="android.widget.SearchView"
as per the documentation.
Upvotes: 2
Reputation: 49
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
Upvotes: 1
Reputation: 1
you can fixed this issue by doing this job in your code
adding this
import android.widget.SearchView;
Upvotes: 0
Reputation: 417
Find import android.widget.SearchView;
in your imports and replace it with import android.support.v7.widget.SearchView
Upvotes: 17
Reputation: 31
Try adding this:
MenuItem menuItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(menuItem);
It worked for me.
Upvotes: 3
Reputation: 2073
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
Upvotes: 44