好的,首先,我知道你以前见过这个问题,我会告诉你为什么这是不同的。我有一个类,DrawView(遵循一些 Canvas 教程),它扩展了 View。好的,但我想要一个单独的类来处理所有动画,所以我可以调用 mainMenuAnimation(),它会绘制它而不是将其编码到实际的游戏循环中。好吧,如果我创建一个用于保存动画的类 Animations.java 并扩展 DrawView,我会从 Eclipse 中得到一个错误:
Implicit super constructor DrawView() is undefined for default constructor. Must define an explicit constructor
问题是,如果我调用 DrawView() 构造函数,它会生成一个新的 Animations.java,依此类推。(也许我应该定义 Animations a = new Animations()?不确定我以后是否会遇到问题)。所以,如果我在 DrawView() 中添加一个空的构造函数,它会给我这个错误:
Implicit super constructor View() is undefined for default constructor. Must define an explicit constructor
我不知道该怎么办,求助?
好的,我在 DrawView() 构造函数中实例化 Animations 的原因是因为 Animations 的构造函数必须是 super(context) 并且访问上下文的唯一方法是通过 DrawView() 构造函数。
DrawView 构造函数代码:
Paint paint; //initialize EVERYTHING
Resources res;
Bitmap title;
Rect titleRect;
boolean inMainMenu, issetBackgroundDrawableSupported;
List<BitmapDrawable> mainMenuAnimation;
int mainMenuAnimationIndex = 0;
public DrawView(Context context) {
super(context);
res = getResources(); //required stuff
title = BitmapFactory.decodeResource(getResources(),R.drawable.title); //title stuff
titleRect = new Rect(res.getDisplayMetrics().widthPixels/2 - title.getWidth()*10 , 100, res.getDisplayMetrics().widthPixels/2 + title.getWidth()*10, 200); //left, top, right, bottom
inMainMenu = false; //main menu stuff
issetBackgroundDrawableSupported = true;
mainMenuAnimation = new ArrayList<BitmapDrawable>();
mainMenuAnimation.add(new BitmapDrawable(getResources(), BitmapFactory.decodeResource(res, R.drawable.mainmenu_background_1)));
mainMenuAnimation.add(new BitmapDrawable(getResources(), BitmapFactory.decodeResource(res, R.drawable.mainmenu_background_2)));
mainMenuAnimation.add(new BitmapDrawable(getResources(), BitmapFactory.decodeResource(res, R.drawable.mainmenu_background_3)));
Animations animations = new Animations(getApplication());
}
和 Animations.java 代码:
public class Animations extends DrawView {
//define animations
@SuppressLint("NewApi")
public void mainMenuScroll(Canvas canvas) {
inMainMenu = true;
//draw main menu here
if (inMainMenu = true) { //main menu loop
if (issetBackgroundDrawableSupported) { //check if background drawing is supported
try {
setBackgroundDrawable(mainMenuAnimation.get(mainMenuAnimationIndex));
} catch (Exception e){
issetBackgroundDrawableSupported = false; //say it is unsupported
setBackground(mainMenuAnimation.get(mainMenuAnimationIndex));
}
}
else {
setBackground(mainMenuAnimation.get(mainMenuAnimationIndex));
}
mainMenuAnimationIndex++;
if (mainMenuAnimationIndex == 3) { //restart main menu animation
mainMenuAnimationIndex = 0;
}
}
}
}
好的,我意识到另一个 Eclipse 通知,可能有用。它说:
Custom view com/spng453/agenericrpg/Animations is missing constructor used by tools: (Context) or (Context,AttributeSet) or (Context,AttributeSet,int)
听起来很相关,但我不知道该怎么做。