Diffusion
Diffusion

Reputation: 46

java: ClassCastException when upcasting Animation to CustomAnimation

I have a class that extends android.view.Animation:

package diffusi.on.com.fifteen_puzzle;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

public class CustomAnimation extends Animation {

    private boolean _isLast = false;

    private View _currentTarget = null;

    public interface AnimationListener {
        void onAnimationEnd(CustomAnimation animation);
        void onAnimationRepeat(CustomAnimation animation);
        void onAnimationStart(CustomAnimation animation);
    }

    public static void animateSetOfViews(
            View[] viewsSet, 
            int animResId, 
            int[] startTimeOffsets,
            Context context,
            AnimationListener animationListener
        ) {
        CustomAnimation animation;
        int startTimeOffset; 
        boolean isLastAnim;

        for (int intA = 0; intA < viewsSet.length; intA++) {
            isLastAnim = intA == viewsSet.length - 1;
            animation = (CustomAnimation) AnimationUtils.loadAnimation(context, animResId);
            if (intA <= startTimeOffsets.length - 1) {
                startTimeOffset = startTimeOffsets[intA];
            } else startTimeOffset = 0;
            animation.applyToView(viewsSet[intA], startTimeOffset, isLastAnim, animationListener);
        }
    }

    public CustomAnimation() {

    }

    public CustomAnimation(Context context, AttributeSet attrs) {
        super(context, attrs);

    }

    public boolean isLast() {
        return this._isLast;
    }

    public View getCurrentTarget() {
        return this._currentTarget;
    }

    private void applyToView(View view, int startTimeOffset, boolean isLast, AnimationListener listener) {
        this._isLast = isLast;
        this._currentTarget = view;
        this.setStartOffset(startTimeOffset);
        this.setAnimationListener((Animation.AnimationListener) listener);
        this._currentTarget.startAnimation(this);
    }

}

It compiles in IDE without errors. But in runtame, it throws an exception (ClassCastEcxeption) on line: animation = (CustomAnimation) AnimationUtils.loadAnimation(context, animResId)

Why does I can't upcast Animation instance to my CustomAnimation, which extends Animation ?

Upvotes: 0

Views: 374

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1499760

It's not upcasting, it is downcasting. Upcasting would be of the form CustomAnimation to Animation.

Presumably AnimationUtils.loadAnimation returns a reference to an object which isn't actually a CustomAnimation - so you can't cast to it. You can only cast to a type when the actual type of the object at execution time is compatible with the type you're casting to. For example:

Object x = new Integer(10);
String y = (String) x; // Bang - invalid cast

Object a = "Foo";
String b = (String) a; // This is fine

Upvotes: 3

Related Questions