Android NullPointerException on ValueAnimator.start()

1.6k views Asked by At

I am using ValueAnimator to animate a View, but it throws a NullPointerException whenever I call start().

here is my stack trace after calling start() in my code:

    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)

here is the part of my code that creates and calls animation:

    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();
2

There are 2 answers

1
TameHog On BEST ANSWER

I'm pretty sure the correct way to create an instance is like this: ValueAnimator va = ValueAnimator.ofInt(someValue, 0);. The va.ofInt(...) is doing nothing right now.

0
braandl On

If you want to use non-static access use

ValueAnimator.setIntValues(int...)