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

2024-04-14

我收到这个错误

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

最后一个参数可以是null,正如所描述的docs https://developer.android.com/reference/android/widget/TextView.OnEditorActionListener.html#onEditorAction(android.widget.TextView,%20int,%20android.view.KeyEvent):

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

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

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

有关平台类型的更多说明,请参阅这个答案 https://stackoverflow.com/a/47480019/4465208.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

java.lang.IllegalArgumentException :指定为非 null 的参数为 null:方法 kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull 的相关文章

随机推荐