最近在用Toolbar+SearchView做搜索,整体效果想做成类微信的那种,在主页面点击搜索图标后弹出搜索页面,然后输入搜索条件进行搜索。但是在引入MenuItemCompat.setOnActionExpandListener时程序出现闪退问题。程序初步效果如下:
build.gradle主要引用的jar包配置如下,由于遇到的问题和jar版本有关所以列出来了。
dependencies {
compile fileTree(
dir
:
'libs'
,
include
: [
'*.jar'
])
testCompile
'junit:junit:4.12'
compile
'com.android.support:appcompat-v7:26.+'
compile
'com.android.support:design:26.+'
compile
'com.android.support:recyclerview-v7:26.+'
}
但是在引入MenuItemCompat.setOnActionExpandListener进行扩展事件引入时出现程序闪退,具体错误如下:
FATAL EXCEPTION: main
Process: com.jxkj.bbccode.anxiuyunslid, PID: 4933
java.lang.UnsupportedOperationException: This is not supported, use MenuItemCompat.setOnActionExpandListener()
at android.support.v7.view.menu.MenuItemImpl.setOnActionExpandListener(MenuItemImpl.java:745)
at android.support.v4.view.MenuItemCompat.setOnActionExpandListener(MenuItemCompat.java:363)
at com.jxkj.bbccode.anxiuyunslid.activity.SearchResultActivity.onCreateOptionsMenu(SearchResultActivity.java:56)
at android.app.Activity.onCreatePanelMenu(Activity.java:3307)
at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:320)
at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:95)
at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:333)
at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:95)
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:6646)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
整个错误困扰了两天,查了大量资料,国内的、国外的没有个确切的解决方法,试试也都没有解决问题。后来在github下载了google官方searchview例子,例子的build.gradle配置如下: 示例源码
dependencies {
compile fileTree(
dir
:
'libs'
,
include
: [
'*.jar'
])
testCompile
'junit:junit:4.12'
compile
'com.android.support:appcompat-v7:24.0.0'
compile
'com.android.support:design:24.0.0'
compile
'com.android.support:recyclerview-v7:24.0.0'
}
在该例程中引入MenuItemCompat.setOnActionExpandListener,一切正常,程序不在闪退崩盘。这里看闪退应该是版本问题。在高版本下如何解决该问题???还没找到方案。
@Override
public boolean
onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.
menu_main
, menu);
MenuItem menuItem=menu.findItem(R.id.
action_search
);
SearchView searchView = (SearchView) MenuItemCompat.
getActionView
(menuItem);
searchView.setOnQueryTextListener(
this
);
MenuItemCompat.
setOnActionExpandListener
(menuItem,
new
MenuItemCompat.OnActionExpandListener() {
@Override
public boolean
onMenuItemActionExpand(MenuItem item) {
return true
;
}
@Override
public boolean
onMenuItemActionCollapse(MenuItem item) {
return true
;
}
});
return super
.onCreateOptionsMenu(menu);
}