有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

MenuItemCompat的java UnsupportedOperationException。setOnActionExpandListener

我有一个用于实现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 安卓.support.v7.view.menu.MenuItemImpl.setOnActionExpandListener(MenuItemImpl.java:743)
                                                                           at 安卓.support.v4.view.MenuItemCompat.setOnActionExpandListener(MenuItemCompat.java:464)
                                                                           at com.troychuinard.fanpolls.NewImageActivity.onCreateOptionsMenu(NewImageActivity.java:86)
                                                                           at 安卓.app.Activity.onCreatePanelMenu(Activity.java:2881)
                                                                           at 安卓.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:328)
                                                                           at 安卓.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:98)
                                                                           at 安卓.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:335)
                                                                           at 安卓.support.v7.app.AppCompatDelegateImplV9.preparePanel(AppCompatDelegateImplV9.java:1368)
                                                                           at 安卓.support.v7.app.AppCompatDelegateImplV9.doInvalidatePanelMenu(AppCompatDelegateImplV9.java:1648)
                                                                           at 安卓.support.v7.app.AppCompatDelegateImplV9$1.run(AppCompatDelegateImplV9.java:140)
                                                                           at 安卓.os.Handler.handleCallback(Handler.java:739)
                                                                           at 安卓.os.Handler.dispatchMessage(Handler.java:95)
                                                                           at 安卓.os.Looper.loop(Looper.java:148)
                                                                           at 安卓.app.ActivityThread.main(ActivityThread.java:5527)
                                                                           at java.lang.reflect.Method.invoke(Native Method)
                                                                           at com.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
                                                                           at com.安卓.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;
        }
    });

共 (1) 个答案

  1. # 1 楼答案

    我在两天前遇到了同样的问题,并通过使用SupportMenuItem而不是MenuItem解决了它。下面的代码显示了我如何使另一个SupportMenuItem的菜单项(in)/可见,以作为展开/折叠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。罐子/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可能不是个好主意?事实上,我的身材。gradle包含“com.android.support:appcompat-v7:26.+”—删除“+”并不会改变什么