异常: 指定为非空的参数为 null: 方法 kotlin.jvm.Internal. intrinsics.checkParameter terIsNotNull

我得到了这个错误

java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter event

这条线

override 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
}
})
90174 次浏览

最后一个参数可以是 null,如 医生所述:

KeyEvent: 如果由输入键触发,则为事件; 否则为空。

所以你要做的就是让 Kotlin 类型为空来解释这个问题,否则注入的 null检查会在你接到一个 null值的电话时崩溃你的应用程序,就像你已经看到的那样:

edtSearch?.setOnEditorActionListener(object : TextView.OnEditorActionListener {
override fun onEditorAction(v: TextView, actionId: Int, event: KeyEvent?): Boolean {
...
}
})

关于 这个答案中平台类型的更多说明。

我得到了一个类似的例外: “ java.lang. IllegalArgumentException: 指定为非空的参数是 null: method kotlin.jvm.Internal. intrinsics.checkParameter terIsNotNull,参数 title”。

然后研究了一个函数,发现一个参数变成了 null,但是不能:

class Item(
val id: Int,
val title: String,
val address: String
)

当我像 Item(id, name, address)namenull那样调用它时,我得到了这个异常。

因此,将 ?添加到所有可疑变量类型: val title: String?

为了解决这个问题,如果您使用 TextView.OnEditorActionListener,请将 event参数设置为可空。在声明的末尾加上 ?:

override fun onEditorAction(v: TextView, actionId: Int, event: KeyEvent?)

如果你将带有 null 值的参数从 Java 类传递到 Kotlin 类(通过调用方法和类之间的实现回调) ,这个错误就会发生。

甚至将 null 传递给编译器在编译时无法检测到的参数,因此在运行时会发生崩溃。

因为 Kotlin 是空安全的,所以应用程序会崩溃!

修正: 将 kotlin 方法中的参数类型更改为 Nullable 类型,方法是在类型的末尾添加? 。

例如,您的 kotlin 函数将在 Java 类中通过以下方式调用:

//java class file
ClassKotlin cls = new ClassKotlin()
cls.function1("name", null, true) //Call func from inside kotlin class

但你在 ClassKotlin 的 func 声明是:

//Kotlin class file
ClassKotlin {
fun function1(firstname : String , lastName : String , status : Bool){
//...
}
} // class

所以您在 kotlin func 中向非 Null 参数传递了一个 Null 值。

解决方法:

只要把 Kotlin 函数改成:

 fun function1(firstname : String , lastName : String? , status : Boolean){
//...
}

添加到 String 数据类型

IsMinifyEnable = false 或删除此

在我的例子中,这个问题是由于在 gradle 文件中的 isMinifyEnabled = true行引起的。

对我来说,它发生在 spinner 适配器项选择器上。 下面是正确的代码。

rootView.spinnerState.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
}
override fun onNothingSelected(parent: AdapterView<*>?) {}
}

确保允许适配器和视图为 null,即(?)

我有点纠结这个问题,我的问题是在 转换器类提供给 房间数据库类。根据 无效性不可否认性的预期值,你可以更仔细地研究一下这个类和 更新变量