我正在尝试在我的菜单栏上实现"搜索窗口"。但是我遇到了错误:" null不能施加到非null type android.widget.widget.searchview"..我确实尝试解决问题,但没有结果。
你能给我一些提示吗?
minsdkversion 17targetsdkversion 26
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.menu, menu)
val searchView = menu!!.findItem(R.id.app_bar_search).actionView as SearchView
val searchManager = getSystemService(Context.SEARCH_SERVICE) as SearchManager
searchView.setSearchableInfo(searchManager.getSearchableInfo(componentName))
searchView.setOnQueryTextListener(object: SearchView.OnQueryTextListener{
override fun onQueryTextSubmit(p0: String?): Boolean {
return false
}
override fun onQueryTextChange(p0: String?): Boolean {
return false
}
})
<item
android:id="@+id/app_bar_search"
android:actionViewClass="android.widget.SearchView"
android:icon="@drawable/ic_search_black_24dp"
android:title="Search"
app:showAsAction="always"/>
尝试更换
android:actionViewClass="android.widget.SearchView"
app:actionViewClass="android.widget.SearchView"