ValueAnimator.start()上的Android NullPointerException

时间:2015-06-09 20:51:42

标签: java android nullpointerexception android-animation

我使用ValueAnimator为View设置动画,但每次调用start()时都会抛出NullPointerException。

这是我的代码中调用start()后的堆栈跟踪:

    java.lang.NullPointerException
        at android.animation.ValueAnimator.initAnimation(ValueAnimator.java:447)
        at android.animation.ValueAnimator.setCurrentPlayTime(ValueAnimator.java:495)
        at android.animation.ValueAnimator.start(ValueAnimator.java:913)
        at android.animation.ValueAnimator.start(ValueAnimator.java:923)

这是我的代码中创建和调用动画的部分:

    ValueAnimator va = new ValueAnimator();
    va.ofInt(somevalue, 0);
    va.setDuration(Game.ANIMATION_DURATION);
    va.addListener(new Animator.AnimatorListener() {
         @Override
         public void onAnimationStart(Animator animation) {//some code here
         }
         @Override
         public void onAnimationEnd(Animator animation) {//some code here
         }

         @Override
         public void onAnimationCancel(Animator animation) {}

         @Override
         public void onAnimationRepeat(Animator animation) {}
    });
    va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
         @Override
         public void onAnimationUpdate(ValueAnimator animation) {
                  //some code here
         }
    });
    va.setInterpolator(new DecelerateInterpolator());
    va.start();

1 个答案:

答案 0 :(得分:3)

我很确定创建实例的正确方法是这样的: ValueAnimator va = ValueAnimator.ofInt(someValue, 0);va.ofInt(...)现在什么也没做。