胖蔡说技术
随便扯扯

Kotlin Parameter specified as non-null is null

报错信息如下:

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")
           ......
         }

     }
 
赞(0) 打赏
转载请附上原文出处链接:胖蔡说技术 » Kotlin Parameter specified as non-null is null
分享到: 更多 (0)

请小编喝杯咖啡~

支付宝扫一扫打赏

微信扫一扫打赏