Kotlin Parameter specified as non-null is null
2017年10月18日 17:21:49 amiko_ 阅读数:9017
版权声明:本文为博主原创文章,未经博主允许不得转载。 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")
......
}
}
原文地址:https://www.cnblogs.com/vana/p/10977418.html
时间: 2024-10-15 00:37:45