Don't run Object animator

267 views Asked by At

Why this code don't run? When run the app, i faced with stopped app error message..

ImageButton img=(ImageButton)findViewById(R.id.img0);
        ObjectAnimator[] imganim = new ObjectAnimator[10];
        imganim[0].setTarget(img);
2

There are 2 answers

0
Joel Duggan On BEST ANSWER

The basic problem with your code, is that you created an array of ObjectAnimator's, but you did not fill the array with actual objects (all array elements are null by default).

Your program is crashing because imganim[0] is null, and you are trying to call a method on a null object (doing so will always result in a crash).

At the very least you need to assign an object into the array element, and then you can access it.

imganim[0] = new ObjectAnimator();
imganim[0].setTarget(img);

Still though, that will just prevent your program from crashing, I'm not really sure what you actually wanted to accomplish. The above code won't actually "do" anything yet.

See http://developer.android.com/reference/android/animation/ObjectAnimator.html for help.

0
nanofarad On

You need to call ObjectAnimator#start() on your instance(s). Otherwise, you will have a constructed and ready-to-start animator that has not been started.