java:ClassCastException将动画上传到CustomAnimation时
我有一个扩展android.view.Animation的类:java:ClassCastException将动画上传到CustomAnimation时,java,android,android-animation,classcastexception,Java,Android,Android Animation,Classcastexception,我有一个扩展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;
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);
}
}
package diffusi.on.com.weeten_拼图;
导入android.content.Context;
导入android.util.AttributeSet;
导入android.view.view;
导入android.view.animation.animation;
导入android.view.animation.AnimationUtils;
公共类CustomAnimation扩展了动画{
私有布尔值_isLast=false;
私有视图_currentTarget=null;
公共界面动画监听器{
无效onAnimationEnd(自定义动画);
void onAnimationRepeat(自定义动画);
void onAnimationStart(自定义动画);
}
公共静态无效动画视图(
视图[]视图集,
国际动物协会,
int[]起始时间偏移量,
语境,
AnimationListener AnimationListener
) {
定制动画;
int STARTTIMETOFFSET;
布尔isLastAnim;
对于(int intA=0;intACustomAnimation
到Animation
大概AnimationUtils.loadAnimation
返回对一个对象的引用,而该对象实际上不是CustomAnimation
——因此您不能对其进行强制转换。只有在执行时对象的实际类型与您要强制转换的类型兼容时,才能强制转换为类型。例如:
Object x = new Integer(10);
String y = (String) x; // Bang - invalid cast
Object a = "Foo";
String b = (String) a; // This is fine
它不是向上投射,而是向下投射。向上投射的形式将是CustomAnimation
到Animation
大概AnimationUtils.loadAnimation
返回对一个对象的引用,而该对象实际上不是CustomAnimation
——因此您不能对其进行强制转换。只有在执行时对象的实际类型与您要强制转换的类型兼容时,才能强制转换为类型。例如:
Object x = new Integer(10);
String y = (String) x; // Bang - invalid cast
Object a = "Foo";
String b = (String) a; // This is fine
你能发布方法loadAnimation吗?我能将AnimationUtils.loadAnimation(上下文,AnimaResid)的结果添加到日志中,看看为什么会出现这个问题。你能发布方法loadAnimation.I能将AnimationUtils.loadAnimation(上下文,AnimaResid)的结果添加到日志中吗看看为什么会出现这个问题。谢谢。术语上的错误谢谢。术语上的错误