NoSuchMethodError when calling MenuItem.collapseActionView

Using the Android Support Library, I implemented an ActionBar (android.support.v7.app.ActionBar). In the meantime, I was trying to interact with SearchView and was trying to collapse SearchItem using the method below:

searchItem.collapseActionView(); 

seachItem is of type MenuItem. This results in the following exception:

 java.lang.NoSuchMethodError: android.view.MenuItem.collapseActionView 

So how do I collapse SearchItem?

+4
source share
1 answer

Instead of using:

 searchItem.collapseActionView(); 

I have to use the static method from android.support.v4.view.MenuItemCompat :

 MenuItemCompat.collapseActionView(searchItem); 

It seems obvious at the moment, but it took several hours to think that the available codes on websites, usually designed to work in the library (android.app.ActionBar) (API 11 and above), did not include the V7 Support Library (android. support.v7.app.ActionBar) , which in some cases has its own methods that are different from each other.

+22
source

Source: https://habr.com/ru/post/1502541/


All Articles