android - 如何修复此错误 java.lang.UnsupportedOperationException: This is not supported, use MenuItemCompat.setOnActionExpandListener()
问题描述
我MenuItemCompat.setOnActionExpandListener ()
在我的 Android 应用程序中使用过。现在我错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
解决方案
您应该使用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;
}
});
推荐阅读
- python - What's an efficient way to stack a multi-level pivot table in Python pandas?
- angularjs - 无法在 Ionic V5 App 上禁用 d3 topojson 底图缩放
- odoo-12 - 根据所选产品类别显示产品
- awk - Column manipulating using Bash & Awk
- java - 为什么 maven 正在编译我的 java 文件,即使它位于 spring boot maven 项目内的不同文件夹中?
- ansible - ansible中的三元运算符
- node.js - 不能在快速路由中执行任何语句,除了最后一个
- elasticsearch - 无法解析日期字段 Elasticsearch
- c++ - 我的程序没有显示任何编译错误,但是当它运行时它每次都突然退出
- css - 如何动态更改 CSS 标签?