I\'m not unsure of why I\'m getting this error. Here\'s the menu in question:
Try adding this:
MenuItem menuItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(menuItem);
It worked for me.
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
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
Find import android.widget.SearchView;
in your imports and replace it with import android.support.v7.widget.SearchView
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
For me i have only changed
app:actionViewClass="android.widget.SearchView"
Insted of
app:actionViewClass="androidx.appcompat.widget.SearchView"