如何修复此错误java.lang.UnsupportedOperationException:这不受支持,请使用MenuItemCompat.setOnActionExpandListener()

问题描述 投票:0回答:1

我在我的Android应用程序中使用了MenuItemCompat.setOnActionExpandListener ()。现在我错了java.lang.UnsupportedOperationException: This is not supported, use MenuItemCompat.setOnActionExpandListener ()给出的。

我的代码:

private void initSearchView(final Menu menu) {
    //Associate searchable configuration with the SearchView
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    MenuItem searchItem = menu.findItem(R.id.action_search);
    if (searchItem != null) {
        searchItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
                    @Override
                    public boolean onMenuItemActionExpand(MenuItem item) {
                        hideFab();
                        return true;
                    }
                    @Override
                    public boolean onMenuItemActionCollapse(MenuItem item) {
                        if (mAdapter != null)
                           mAdapter.expandAll();
                        showFab();
                        return true;
                    }
                });
        mSearchView = (SearchView) searchItem.getActionView();
        mSearchView.setInputType(InputType.TYPE_TEXT_VARIATION_FILTER);
        mSearchView.setImeOptions(EditorInfo.IME_ACTION_DONE | EditorInfo.IME_FLAG_NO_FULLSCREEN);
        mSearchView.setQueryHint(getString(R.string.action_search));
        mSearchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        mSearchView.setOnQueryTextListener(this);
    }
}

发生了错误:

04-07 15:18:11.556 18945-18945/com.blackswan.shaghayeghaccounting.demo E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.blackswan.shaghayeghaccounting.demo, PID: 18945
java.lang.UnsupportedOperationException: This is not supported, use MenuItemCompat.setOnActionExpandListener()
    at android.support.v7.view.menu.MenuItemImpl.setOnActionExpandListener(MenuItemImpl.java:740)
    at com.blackswan.shaghayeghaccounting.base.activities.BaseListActivity.initSearchView(BaseListActivity.java:512)
    at com.blackswan.shaghayeghaccounting.base.activities.BaseListActivity.onCreateOptionsMenu(BaseListActivity.java:480)
    at com.blackswan.shaghayeghaccounting.transactions.TransactionActivity.onCreateOptionsMenu(TransactionActivity.java:483)
    at android.app.Activity.onCreatePanelMenu(Activity.java:3142)
    at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:328)
    at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:88)
    at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:270)
    at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:88)
    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:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:7325)
    at java.lang.reflect.Method.invoke(Native Method)
    at 
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
04-07 15:18:11.591 2820-19251/? E/android.os.Debug: ro.product_ship = true
04-07 15:18:11.591 2820-19251/? E/android.os.Debug: ro.debug_level = 0x4f4c
04-07 15:18:11.591 2820-19251/? E/android.os.Debug: sys.mobilecare.preload = false
04-07 15:18:12.871 18945-19248/com.blackswan.shaghayeghaccounting.demo 
E/FirebaseCrash: Unable to parse Json response string to get message: No value for crashes
04-07 15:18:13.971 2820-2946/? E/ViewRootImpl: sendUserActionEvent() mView == null
android
1个回答
0
投票

你应该使用MenuItemCompat来设置OnActionExpandListener

MenuItemCompat.setOnActionExpandListener(searchItem,
    new MenuItemCompat.OnActionExpandListener() {
        @Override
        public boolean onMenuItemActionExpand(MenuItem menuItem) {
            hideFab();
            return true;
        }
        @Override
        public boolean onMenuItemActionCollapse(MenuItem menuItem) {
            if (mAdapter != null)
               mAdapter.expandAll();
            showFab();
            return true;
        }
    });
© www.soinside.com 2019 - 2024. All rights reserved.