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平台类型的更多说明.

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...