简体   繁体   English

Android:未为默认构造函数定义隐式超级构造函数

[英]Android: Implicit super constructer undefined for default constructer

Ok, first of all, I know you have seen this problem before, and I'll tell you why this is different. 好的,首先,我知道您以前已经遇到过此问题,我将告诉您为什么这与众不同。 I have a class, DrawView (followed some Canvas tutorials) and it extends View. 我有一个DrawView类(后面有一些Canvas教程),它扩展了View。 Ok, but I want a separate class to handle all the animations, so I can just call, for example, mainMenuAnimation() and it will draw it instead of coding it to the actual game loop. 好的,但是我想要一个单独的类来处理所有动画,因此我可以仅调用例如mainMenuAnimation(),它将对其进行绘制,而不是将其编码为实际的游戏循环。 Well, if I create a class for holding the animations, Animations.java, and extend DrawView, I get an error from Eclipse: 好吧,如果我创建一个用于保存动画的类Animations.java并扩展DrawView,则会从Eclipse中收到错误消息:

Implicit super constructor DrawView() is undefined for default constructor. Must define an explicit constructor

The problem is, if I call the DrawView() constructor, it makes a new Animations.java, and so on. 问题是,如果我调用DrawView()构造函数,它将生成一个新的Animations.java,依此类推。 (Maybe I should define Animations a = new Animations()? Not sure if I would run into problems later on though). (也许我应该定义Animations a = new Animations()?不过不确定以后是否会遇到问题)。 So, if I add an empty constructor in DrawView(), it gives me this error: 因此,如果我在DrawView()中添加一个空的构造函数,则会出现此错误:

    Implicit super constructor View() is undefined for default constructor. Must define an explicit constructor

I have no idea what to do, help? 我不知道该怎么办,有帮助吗?

Okay, the reason why I instanced Animations in the DrawView() constructor is because Animations' constructor has to be super(context) and the only way to access the context is through the DrawView() constructor. 好的,之所以在DrawView()构造函数中实例化Animations的原因是因为Animations的构造函数必须是super(context),并且访问上下文的唯一方法是通过DrawView()构造函数。

DrawView constructor code: 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());
}

And the Animations.java code: 还有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;
        }
    }
}

} }

Ok, I realized another Eclipse notification, might be useful. 好的,我意识到另一个Eclipse通知可能很有用。 It says: 它说:

Custom view com/spng453/agenericrpg/Animations is missing constructor used by tools: (Context) or (Context,AttributeSet) or (Context,AttributeSet,int)

Sounds relevant, but I'm not sure what to do about it. 听起来很相关,但是我不确定该怎么做。

All View s run within the context of a Context. 所有View都在Context的上下文中运行。 (I guess that's why it's called that =P). (我想这就是为什么将其称为= P的原因)。 This includes your custom View . 这包括您的自定义视图

You're going to want to define an Animations constructor that takes a Context , so you can pass it through to the super constructors. 您将要定义一个采用ContextAnimations构造函数,以便可以将其传递给super构造函数。 This is the cleanest way to get rid of your errors, and will also fix the last problem you mentioned (namely, the Android system is trying to instantiate your class, but it doesn't know what to do with a View that doesn't take a Context in its constructor). 这是消除错误的最干净的方法,并且还可以解决您提到的最后一个问题(即,Android系统正在尝试实例化您的类,但是它不知道如何处理不包含View在其构造函数中使用Context )。

public Animations(Context context) {
    super(context);  
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM