我正在使用一个实现SearchView的活动。
加载活动时,我立即看到以下错误:
11-22 20:55:21.013 10008-10008/com.troychuinard.fanpolls E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.troychuinard.fanpolls, PID: 10008
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:464)
at com.troychuinard.fanpolls.NewImageActivity.onCreateOptionsMenu(NewImageActivity.java:86)
at android.app.Activity.onCreatePanelMenu(Activity.java:2881)
at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:328)
at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:98)
at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:335)
at android.support.v7.app.AppCompatDelegateImplV9.preparePanel(AppCompatDelegateImplV9.java:1368)
at android.support.v7.app.AppCompatDelegateImplV9.doInvalidatePanelMenu(AppCompatDelegateImplV9.java:1648)
at android.support.v7.app.AppCompatDelegateImplV9$1.run(AppCompatDelegateImplV9.java:140)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5527)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
我不知道它是否与我正在使用的Android API有关,但基本上我不知道要更改相应的行以删除错误:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_search, menu);
// Retrieve the SearchView and plug it into SearchManager
MenuItem searchItem = menu.findItem(R.id.search);
final SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchItem.expandActionView();
MenuItemCompat.setOnActionExpandListener(searchItem, new MenuItemCompat.OnActionExpandListener() {
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
Toast.makeText(getApplicationContext(), "Open", Toast.LENGTH_LONG).show();
return true;
}
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
Toast.makeText(getApplicationContext(), "Back_Press", Toast.LENGTH_LONG).show();
finish();
return true;
}
});
答案 0 :(得分:0)
我在2天前遇到了同样的情况,并使用SupportMenuItem而不是MenuItem解决了这个问题。下面的代码显示了我如何为我的FilterView创建另一个SupportMenuItem的菜单项(in)/ visible作为扩展/折叠SearchView的反应。
final SupportMenuItem searchItem = (SupportMenuItem) menu.findItem(R.id.action_search);
searchItem.setSupportOnActionExpandListener(new MenuItem.OnActionExpandListener() {
@Override
public boolean onMenuItemActionExpand(MenuItem menuItem) {
filterItem.setVisible(false);
return true;
}
@Override
public boolean onMenuItemActionCollapse(MenuItem menuItem) {
filterItem.setVisible(true);
//adp.cleanMap();
return true;
}
});
以下是我的解决方案: 关于bug in the 26.0.0-alpha1 release有一个提示。调试我的代码,我发现我的问题也可能与此相关,如代码路径“appcompat-v7 / 26.0.0-alpha1 / appcompat-v7-26.0.0-alpha1-sou> rces.jar!/android/support/v7/view/menu/MenuItemImpl.java“显示在调试器中。检查该源文件显示我们正在运行的消息:
@Override
public MenuItem setOnActionExpandListener(MenuItem.OnActionExpandListener listener) {
throw new UnsupportedOperationException(
"This is not supported, use MenuItemCompat.setOnActionExpandListener()");
}
然后我在该源中找到了以下函数,这让我试一试
@Override
public SupportMenuItem setSupportOnActionExpandListener(
MenuItem.OnActionExpandListener listener) {
mOnActionExpandListener = listener;
return this;
}
如上所述,对我来说非常适合。对此有何评论?使用SupportMenuItem可能是个坏主意吗?实际上,我的build.gradle包含“com.android.support:appcompat-v7:26.+” - 删除“+”并没有改变。