我正在尝试在菜单上初始化自定义搜索工具(3d 库),但出现错误。
这是我的代码
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.base_nav_drawer,menu)
searchItem= menu?.findItem(R.id.srchbar_menu)!!
global_search= searchItem.actionView as MaterialSearchBar
lastsuggestions=ArrayList<String>()
loadSuggest()
global_search.lastSuggestions=lastsuggestions
return true
}
日志猫消息
kotlin.TypeCastException: null cannot be cast to non-null type com.mancj.materialsearchbar.MaterialSearchBar
at com.example.sg772.foodorder.BaseNavDrawerActivity.onCreateOptionsMenu(BaseNavDrawerActivity.kt:136)
我试过这个
global_search= searchItem.actionView as? MaterialSearchBar
但它没有帮助第 136 行对应于global_search= searchItem.actionView as MaterialSearchBar
菜单的xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/srchbar_menu"
android:title="search"
android:icon="@drawable/ic_search_black_24dp"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="com.mancj.materialsearchbar.MaterialSearchBar"/>
</menu>