java.lang.IllegalArgumentException:指定为非null的参数为null:method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull

Nir*_*bag 27 android illegalargumentexception kotlin

我收到了这个错误

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
    }
})
Run Code Online (Sandbox Code Playgroud)

zsm*_*b13 30

最后一个参数可以是null,如文档所述:

KeyEvent:如果由回车键触发,则为事件; 否则,这是null.

所以你需要做的就是让Kotlin类型为空可以解释这个问题,否则注入的null检查会在你的应用程序获得一个带有null你已经看过的值的调用时崩溃:

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

有关此答案中平台类型的更多说明.


Bha*_*ani 16

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

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

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


小智 6

要解决此问题,请使event参数可以为空(如果使用TextView.OnEditorActionListener. ?在声明的末尾添加:

override fun onEditorAction(v: TextView, actionId: Int, event: KeyEvent?)
Run Code Online (Sandbox Code Playgroud)


Ham*_*ani 6

如果您将带有 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
Run Code Online (Sandbox Code Playgroud)

但 ClassKotlin 中的 func 声明是:

//Kotlin class file
  ClassKotlin {
  fun function1(firstname : String , lastName : String , status : Bool){
  //...
  }
 } // class
Run Code Online (Sandbox Code Playgroud)

因此,您将 null 值传递给 kotlin func 中的非 Null 参数。

怎么修:

只需将 Kotlin 函数更改为:

 fun function1(firstname : String , lastName : String? , status : Boolean){
  //...
  }
Run Code Online (Sandbox Code Playgroud)

* A ?添加到字符串数据类型


Coo*_*ind 5

我得到了类似的异常:“java.lang.IllegalArgumentException:指定为非空的参数为空:方法 kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull,参数title”。

然后研究了一个函数,发现有一个参数变成了nullwhile 不得:

class Item(
    val id: Int,
    val title: String,
    val address: String
)
Run Code Online (Sandbox Code Playgroud)

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

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