无法设置OnActionExpandListener:不支持此功能,请使用MenuItemCompat.setOnActionExpandListener()

时间:2017-07-26 19:28:27

标签: android android-support-library android-menu android-search

这听起来像是一个重复的问题,但事实并非如此。

我正在尝试将OnActionExpandListener设置为我的搜索菜单项:

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;
}

当我运行时,我得到了这个例外:

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)

很酷,让我们再使用MenuItemCompat.setOnActionExpandListener()

以下是我用menuItem.setOnActionExpandListener(this);替换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:

尝试使用A:不支持此功能,请使用B.

尝试使用B:不支持此功能,请使用B.

我的一些build.gradle配置可能是相关的:

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()已被弃用,文档建议使用MenuItem.setOnActionExpandListener(MenuItem.OnActionExpandListener,这是我首先尝试过的,但正如您所看到的,它不起作用。

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

com.android.support:support-annotations:26.0.0-alpha1 已离开alpha版本。尝试更新到 com.android.support:support-annotations:26.0.0

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