Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't set OnActionExpandListener: This is not supported, use MenuItemCompat.setOnActionExpandListener()

This sounds like a duplicate question, but it isn't.

I'm trying to set an OnActionExpandListener to my search menu item:

public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu_country_picker, menu);
    MenuItem menuItem = menu.findItem(R.id.search);
    menuItem.setOnActionExpandListener(this);
    //more code
    return true;
}

When I run that, I get this exception:

java.lang.UnsupportedOperationException: This is not supported, use MenuItemCompat.setOnActionExpandListener()
                                                                            at android.support.v7.view.menu.MenuItemImpl.setOnActionExpandListener(MenuItemImpl.java:743)
                                                                            at com.myapp.pages.country_picker.CountryPickerActivity.onCreateOptionsMenu(CountryPickerActivity.java:59)
                                                                            at android.app.Activity.onCreatePanelMenu(Activity.java:3145)
                                                                            at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:320)
                                                                            at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:92)
                                                                            at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:331)
                                                                            at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:92)
                                                                            at android.support.v7.app.ToolbarActionBar.populateOptionsMenu(ToolbarActionBar.java:454)
                                                                            at android.support.v7.app.ToolbarActionBar$1.run(ToolbarActionBar.java:61)
                                                                            at android.os.Handler.handleCallback(Handler.java:751)
                                                                            at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                            at android.os.Looper.loop(Looper.java:154)
                                                                            at android.app.ActivityThread.main(ActivityThread.java:6121)
                                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)

Cool, let's use MenuItemCompat.setOnActionExpandListener() then!

Here's what I get when I replace menuItem.setOnActionExpandListener(this); with MenuItemCompat.setOnActionExpandListener(menuItem, this);:

java.lang.UnsupportedOperationException: This is not supported, use MenuItemCompat.setOnActionExpandListener()
                                                                            at android.support.v7.view.menu.MenuItemImpl.setOnActionExpandListener(MenuItemImpl.java:743)
                                                                            at android.support.v4.view.MenuItemCompat.setOnActionExpandListener(MenuItemCompat.java:363)
                                                                            at com.myapp.pages.country_picker.CountryPickerActivity.onCreateOptionsMenu(CountryPickerActivity.java:57)
                                                                            at android.app.Activity.onCreatePanelMenu(Activity.java:3145)
                                                                            at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:320)
                                                                            at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:92)
                                                                            at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:331)
                                                                            at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:92)
                                                                            at android.support.v7.app.ToolbarActionBar.populateOptionsMenu(ToolbarActionBar.java:454)
                                                                            at android.support.v7.app.ToolbarActionBar$1.run(ToolbarActionBar.java:61)
                                                                            at android.os.Handler.handleCallback(Handler.java:751)
                                                                            at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                            at android.os.Looper.loop(Looper.java:154)
                                                                            at android.app.ActivityThread.main(ActivityThread.java:6121)
                                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)

TLDR:

Trying to use A: This is not supported, use B.

Trying to use B: This is not supported, use B.

Some of my build.gradle config that might be relevant:

compileSdkVersion 23
buildToolsVersion '25.0.2'

defaultConfig {
    minSdkVersion 21
    targetSdkVersion 23
}

dependencies {
    compile 'com.android.support:support-v4:23.4.0'
    androidTestCompile 'com.android.support:support-annotations:26.0.0-alpha1'
    ...
}

PS: MenuItemCompat.setOnActionExpandListener() is deprecated and the docs recommend to use MenuItem.setOnActionExpandListener(MenuItem.OnActionExpandListener, which is what I tried in the first place, but as you can see, it didn't work.

Any ideas?

like image 386
Mateus Gondim Avatar asked Jul 26 '17 19:07

Mateus Gondim


1 Answers

com.android.support:support-annotations:26.0.0-alpha1 has left alpha version. Try updating to com.android.support:support-annotations:26.0.0

https://developer.android.com/topic/libraries/support-library/packages.html#annotations

like image 118
manusobles Avatar answered Nov 05 '22 18:11

manusobles