androidx.appcompat.widget.SearchView cannot be cast to androidx.core.view.ActionProvider

Faced an error in the onCreateOptionsMenu method during menu inflate. I do not understand why he refers to this field – it seems that I brought everything and did it correctly under androidx.

public boolean onCreateOptionsMenu(Menu menu) {

        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_main, menu);
        SearchManager searchManager = (SearchManager) getSystemService(SEARCH_SERVICE);
        SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
        MenuItem searchMenuItem = menu.findItem(R.id.action_search);

        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        searchView.setQueryHint("Поиск последний новостей ...");
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                if (query.length() > 2) {
                    onLoadingSwipeRefresh(query);
                }
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                return false;
            }
        });

        searchMenuItem.getIcon().setVisible(false, false);

        return true;
    }

menu_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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=".activities.MainActivity">

    <item
        android:id="@+id/action_search"
        android:orderInCategory="100"
        android:title="Search"
        android:icon="@drawable/ic_search"
        app:showAsAction="always"
        app:actionProviderClass="androidx.appcompat.widget.SearchView"
        android:theme="@android:style/Theme.Holo.Light.DarkActionBar"
        />

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

    <item
        android:id="@+id/action_weather"
        android:orderInCategory="100"
        android:title="Weather"
        android:icon="@drawable/ic_weather"
        app:showAsAction="always" />

</menu>

How can this be fixed? Thank you in advance! P.S. A log with a description of the error is listed below:

Process: com.sv.newsapp, PID: 16248
    java.lang.ClassCastException: androidx.appcompat.widget.SearchView cannot be cast to androidx.core.view.ActionProvider
        at androidx.appcompat.view.SupportMenuInflater$MenuState.readItem(SupportMenuInflater.java:425)
        at androidx.appcompat.view.SupportMenuInflater.parseMenu(SupportMenuInflater.java:179)
        at androidx.appcompat.view.SupportMenuInflater.inflate(SupportMenuInflater.java:129)
        at com.sv.newsapp.activities.MainActivity.onCreateOptionsMenu(MainActivity.java:150)

Answer

You need to use app:actionViewClass, not app:actionProviderClass, as SearchView is a CollapsibleActionView, not an ActionProvider.

<item
    android:id="@+id/action_search"
    android:orderInCategory="100"
    android:title="Search"
    android:icon="@drawable/ic_search"
    app:showAsAction="always"
    app:actionViewClass="androidx.appcompat.widget.SearchView"
    android:theme="@android:style/Theme.Holo.Light.DarkActionBar"
    />

Leave a Reply

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