码迷,mamicode.com
首页 > 其他好文 > 详细

kotlin 简单处理 回调参数 加?

时间:2019-06-05 10:07:20      阅读:91      评论:0      收藏:0      [点我收藏+]

标签:contex   rri   use   down   private   context   views   follow   info   

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

kotlin 简单处理 回调参数 加?

标签:contex   rri   use   down   private   context   views   follow   info   

原文地址:https://www.cnblogs.com/vana/p/10977418.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!