java.lang.IllegalArgumentException:指定为非空的参数为空:方法 kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull [英] java.lang.IllegalArgumentException : Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull
问题描述
我收到此错误
java.lang.IllegalArgumentException:参数指定为非空为空:方法 kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull,参数事件
线
覆盖 fun onEditorAction(v: TextView, actionId: Int, event: KeyEvent)
以下是完整的代码.这段代码最初是在 Java 中编写的,我使用 Android Studio 将其转换为 Kotlin,但现在出现此错误.我尝试重建和清理项目,但没有奏效.
Following is the entire code. This code was originally in java, I converted it to Kotlin using Android Studio, but now I'm getting this error. I tried rebuilding and cleaning the project, but that didn't work.
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
,如文档:
The last parameter can be null
, as described by the docs:
KeyEvent
:如果由回车键触发,则为该事件;否则,这是空的.
KeyEvent
: If triggered by an enter key, this is the event; otherwise, this is null.
所以你必须做的是让 Kotlin 类型可以为空来解决这个问题,否则注入的 null
检查会让你的应用程序在收到带有 null
您已经看到的价值:
So what you have to do is make the Kotlin type nullable to account for this, otherwise the injected null
check will crash your application when it gets a call with a null
value as you've already seen it:
edtSearch?.setOnEditorActionListener(object : TextView.OnEditorActionListener {
override fun onEditorAction(v: TextView, actionId: Int, event: KeyEvent?): Boolean {
...
}
})
在这个答案中有更多关于平台类型的解释.
More explanation about platform types in this answer.
这篇关于java.lang.IllegalArgumentException:指定为非空的参数为空:方法 kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!