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

标签 android kotlin illegalargumentexception

我收到了这个错误

java.lang.IllegalArgumentException:指定为非空的参数为空:方法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 类型可以为空来解决这个问题,否则注入(inject)的 null 检查会在你的应用程序收到带有 null 的调用时崩溃> 你已经看到的值(value):

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

更多关于平台类型的解释见 this answer .

关于android - java.lang.IllegalArgumentException : Parameter specified as non-null is null: method kotlin. jvm.internal.Intrinsics.checkParameterIsNotNull,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47849219/

相关文章:

Android:通过使用标签而不是 ID 查找容器来添加 fragment

android - K9邮件客户端有没有提供通过广播intent获取邮件内容的方法?

android - 点击声音无法播放android onClick

kotlin - 改造成功后,协程问题将数据插入房间数据库

android - 使用 ionic 框架,本地镜像在 chrome 中完美显示,但在我的 android 手机中不显示

android - 播放单轨Spotify SDK

android - 在 KitKat 4.4.4 API 19 上使用改造上传图像时应用程序崩溃

java - HTTP 客户端不工作

android - IllegalArgumentException:在 Google MapView 的 zoomOut 期间宽度和高度必须 > 0

java.lang.illegalargumentException 没有为该名称定义查询[...]