这似乎是一个重复的问题,但它并不是。
我正在尝试将 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()
好了!当我用 MenuItemCompat.setOnActionExpandListener(menuItem, this);
替换 menuItem.setOnActionExpandListener(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)
简述:
尝试使用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)
,这也是我一开始尝试的方法,但是显然它没有起作用。
你有什么想法吗?