kotlin 简单处理 回调参数 加?

Kotlin Parameter specified as non-null is null

 版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chf1142152101/article/details/78275298

报错信息如下:


java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter animation at cn.enjoytoday.expandmateriallayout.ExpandRefreshLayout$mRefreshListener$1.onAnimationEnd(ExpandRefreshLayout.kt:0) at cn.enjoytoday.expandmateriallayout.ExpandRefreshLayout$HeadViewContainer.onAnimationEnd(ExpandRefreshLayout.kt:1099) at android.view.ViewGroup.finishAnimatingView(ViewGroup.java:6293) at android.view.View.draw(View.java:17180) ......

kotlin 中对于回调对象若是为说明可以为空的情况下,kotlin 会自动对齐对象进行非空检查,就会报出如上错误,检查代码发现是设置接口的参数和创建的接口的回调参数的类型设置不一致,如下:


//创建的接口:
private animationListener = object: Animation.AnimationListener {
         override fun onAnimationStart(animation:Animation) {
          ......
        }


        override fun onAnimationRepeat(animation:Animation) {} override fun onAnimationEnd(animation:Animation) { log(message = "onAnimationEnd") ...... } } //监听的类的声明 class CustomLayout(context:Context):LinearLayout(context){ private var listener: Animation.AnimationListener? = null fun setAnimationListener(listener: Animation.AnimationListener?) { this.listener = listener } public override fun onAnimationStart() { super.onAnimationStart() log(message = "onAnimationStart animation is null :${animation==null}") listener?.onAnimationStart(this.animation) } public override fun onAnimationEnd() { super.onAnimationEnd() log(message = "onAnimationEnd animation is null :${animation==null}") listener?.onAnimationEnd(this.animation) } } //使用 fun useMethod(){ val layout=CustomLayout(context) val animation=ScaleAnimation(1f, 0f, 1f, 1f, Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT, 0.5f); layout.setAnimationListener(animationListener) layout.clearAnimation() layout.startAnimation(animation) }

如上代码所示,通过运行 useMethod 方法,出现 “Parameter specified as non-null is null”报错,解决方法很简单,将我们设置的接口回调参数设置为可空类型即可,如下:


//创建的接口,目前就log看可知,onAnimationStart时animation为非空,onAnimationEnd为空
//因此,也可单独只对onAnimationEnd(animation:Animation)修改.
private animationListener = object: Animation.AnimationListener {
         override fun onAnimationStart(animation:Animation?) { ...... } override fun onAnimationRepeat(animation:Animation?) {} override fun onAnimationEnd(animation:Animation?) { log(message = "onAnimationEnd") ...... } }
 

 

Enjoytoday,EnjoyCoding

转载于:https://www.cnblogs.com/vana/p/10977418.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值