【问题标题】:java.lang.IllegalArgumentException : Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNulljava.lang.IllegalArgumentException:指定为非空的参数为空:方法 kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull
【发布时间】:2018-05-30 15:50:25
【问题描述】:

我收到了这个错误

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
    }
})

【问题讨论】:

    标签: android kotlin illegalargumentexception


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:

      如果您将带有 null 值的参数从 Java 类传递给 Kotlin 类[通过调用方法并在类之间实现回调],则会发生此错误。

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

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

      修复:通过添加将 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){
        //...
        }
      

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

      【讨论】:

        【解决方案3】:

        isMinifyEnabled = false 或删除它

        在我的情况下,这个问题是由于 gradle 文件中的isMinifyEnabled = true 行引起的。删除此行后,它就可以工作了。

        【讨论】:

        • 如果您想缩小和发布您的应用程序,这将是必需的,这不是一个有效的解决方案
        【解决方案4】:

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

        然后研究了一个函数,发现一个参数变成了null while must not:

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

        当我将它称为 Item(id, name, address) 并且 namenull 时,我得到了这个异常。

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

        【讨论】:

        • 如果title 可以为空,那么您应该将title 的类型更改为String?。 ? 表示参数是可空类型,这意味着它可能为空。那么当你访问它时,你需要考虑它为空的可能性。
        • @SamIAmHarris,当然。我不知道,title 可能是null(它来自 JSON)。所以,我改成了String?。我写这个答案是为了说明如何抛出这个异常。
        【解决方案5】:

        若要解决此问题,请将 event 参数设为可为空,如果您使用 TextView.OnEditorActionListener。在声明末尾添加?

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

        【讨论】:

          【解决方案6】:

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

          KeyEvent:如果被回车键触发,这就是事件;否则为空。

          所以你需要做的是让 Kotlin 类型可以为空来解决这个问题,否则注入的 null 检查将在你的应用程序收到带有 null 值的调用时崩溃,正如你已经看到的那样:

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

          this answer 中有关平台类型的更多说明。

          【讨论】:

          • 这段代码onEditorAction(v: TextView, actionId: Int, event: KeyEvent)是否插入IDE?如果是这样,那就是IDE的错误......
          猜你喜欢
          • 2019-05-16
          • 2019-06-03
          • 1970-01-01
          • 2020-07-26
          • 1970-01-01
          • 2022-10-07
          • 1970-01-01
          • 2017-12-06
          相关资源
          最近更新 更多