我收到了这个错误
java.lang.IllegalArgumentException:指定为非null的参数为null:方法kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull,参数事件
为线
覆盖fun onEditorAction(v:TextView,actionId:Int,event:KeyEvent)
以下是整个代码.这段代码最初是在java中,我使用Android Studio将其转换为Kotlin,但现在我收到了这个错误.我尝试重建和清理项目,但这没有用.
val action = supportActionBar //get the actionbar action!!.setDisplayShowCustomEnabled(true) //enable it to display a custom view in the action bar. action.setCustomView(R.layout.search_bar)//add the custom view action.setDisplayShowTitleEnabled(false) //hide the title edtSearch = action.customView.findViewById(R.id.edtSearch) as EditText //the text editor //this is a listener to do a search when the user clicks on search button edtSearch?.setOnEditorActionListener(object : TextView.OnEditorActionListener { override fun onEditorAction(v: TextView,actionId: Int,event: KeyEvent): Boolean { if (actionId == EditorInfo.IME_ACTION_SEARCH) { Log.e("TAG","search button pressed") //doSearch() return true } return false } })
解决方法
最后一个参数可以为null,如
docs所述:
KeyEvent
: If triggered by an enter key,this is the event; otherwise,this is null.
所以你需要做的就是让Kotlin类型为空可以解释这个问题,否则注入的null检查会在你的应用程序获得一个带有null值的调用时崩溃,就像你已经看到它一样:
edtSearch?.setOnEditorActionListener(object : TextView.OnEditorActionListener { override fun onEditorAction(v: TextView,event: KeyEvent?): Boolean { ... } })
有关this answer平台类型的更多说明.