微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我收到了这个错误

java.lang.IllegalArgumentException:指定为非null的参数为null:方法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类型为空可以解释这个问题,否则注入的null检查会在你的应用程序获得一个带有null值的调用时崩溃,就像你已经看到它一样:

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐