zoukankan      html  css  js  c++  java
  • 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")
              ......
            }
    
        }
    

    Enjoytoday,EnjoyCoding

  • 相关阅读:
    UISlider
    App两个页面之间的正反传值方法
    UIImageview的简单运用
    UIPickerview 基本使用
    IOS开发中用开关(UISwitch)跟滑块(UISlider)控制手机屏幕的亮度
    冒泡排序
    简单抽屉实现
    iOS 模态视图,视图之间的切换
    UIScrollView和UIPageControl的使用(实现图片的循环滚动)
    iOS中UIPickerView实现省/市连动
  • 原文地址:https://www.cnblogs.com/amiko/p/7906208.html
Copyright © 2011-2022 走看看