Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.UnsupportedOperationException: This is not supported, use MenuItemCompat.setOnActionExpandListener()

Tags:

android

I have recently migrated from Eclipse to Android Studio and in doing so I have picked up the below error

java.lang.UnsupportedOperationException: This is not supported, use MenuItemCompat.setOnActionExpandListener()
        at android.support.v7.internal.view.menu.MenuItemImpl.setOnActionExpandListener(MenuItemImpl.java:740)
        at biz.nickbullcomputing.bevnav.MainActivity.onCreateOptionsMenu(MainActivity.java:699)
        at android.app.Activity.onCreatePanelMenu(Activity.java:2851)
        at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:277)
        at android.support.v7.internal.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:84)
        at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:273)
        at android.support.v7.app.AppCompatDelegateImplV7.preparePanel(AppCompatDelegateImplV7.java:1111)
        at android.support.v7.app.AppCompatDelegateImplV7.doInvalidatePanelMenu(AppCompatDelegateImplV7.java:1396)
        at android.support.v7.app.AppCompatDelegateImplV7.access$100(AppCompatDelegateImplV7.java:89)
        at android.support.v7.app.AppCompatDelegateImplV7$1.run(AppCompatDelegateImplV7.java:126)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:155)
        at android.app.ActivityThread.main(ActivityThread.java:5725)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1030)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:825)

This error appears to becoming from the following code snippet of my main activity

 searchItem = menu.findItem(R.id.action_search);

     searchItem.setOnActionExpandListener(new OnActionExpandListener()
     {
         @Override
         public boolean onMenuItemActionCollapse(MenuItem item) {
             townList.setVisibility(View.INVISIBLE);
             return true;       // Return true to collapse action view
         }
         @Override
         public boolean onMenuItemActionExpand(MenuItem item) {
             townList.setVisibility(View.VISIBLE);
             return true;      // Return true to expand action view
         }
     });

The search's xml code:

<item android:id="@+id/action_search"
    android:icon="@drawable/ic_action_search"
    android:title="@string/action_search"
    app:showAsAction="ifRoom|collapseActionView"
    app:actionViewClass="android.support.v7.widget.SearchView"/>

My build.gradle file's dependencies

dependencies {
compile 'com.android.support:support-v4:22.2.1'
compile 'com.android.support:appcompat-v7:22.0.0'
compile 'com.google.android.gms:play-services:+'

}

Now keep in mind before the migration this was working perfectly and now it's not. I'm not entirely sure how this has happened. Any ideas?

like image 851
Nick Bull Avatar asked Aug 23 '15 15:08

Nick Bull


1 Answers

The fix thanks to ρяσѕρєя K's comment. Much appreciated mate, thank you!!

MenuItemCompat.setOnActionExpandListener(searchItem,
            new MenuItemCompat.OnActionExpandListener() {
                @Override
                public boolean onMenuItemActionExpand(MenuItem menuItem) {
                    // Return true to allow the action view to expand
                    townList.setVisibility(View.VISIBLE);
                    return true;
                }
                @Override
                public boolean onMenuItemActionCollapse(MenuItem menuItem) {
                    // When the action view is collapsed, reset the query
                    townList.setVisibility(View.INVISIBLE);
                    // Return true to allow the action view to collapse
                    return true;
                }
            });
like image 73
Nick Bull Avatar answered Oct 22 '22 14:10

Nick Bull