android – SearchView using AppCompat-ThrowExceptions

Exception or error:

I implemented SearchView in Actionbar before using appcompat.v7
but when I want to use the SearchView with support library v7 it shows null exception

In style

<item android:id="@+id/action_search"
    android:actionViewClass="" />

In Java Class:

public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(, menu);
    SearchView searchView = (SearchView) menu.findItem(;
    return super.onCreateOptionsMenu(menu);

My question is how to declare SearchView in onCreateOptionsMenu to be able to set query listener?

How to solve:

You should use the static methods in MenuItemCompat do deal with all AppCompat menu items. This was mentioned in this blog post as the last item under ‘New Integration’. Just replace your SearchView declaration with the following.

SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(;

Here’s a link to the MenuItemCompat documentation.

I just assumed you are using the latest version of AppCompat with the support for the new Toolbar widget.


If you change android:actionViewClass to app:actionViewClass your existing code will continue working.

Leave a Reply

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