1

さて、まず第一に、あなたが以前にこの問題を見たことがあることを私は知っています、そして私はこれがなぜ違うのかをあなたに話します。私はクラス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などが作成されることです。(たぶん、アニメーション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)

関連性があるように聞こえますが、どうしたらよいかわかりません。

4

1 に答える 1

1

すべてViewの s は Context のコンテキスト内で実行されます。(それが =P と呼ばれる理由だと思います)。これにはカスタム View が含まれます

Animationsを受け取るコンストラクターを定義しContextて、それをスーパー コンストラクターに渡すことができます。これはエラーを取り除くための最もクリーンな方法であり、あなたが言及した最後の問題も修正します (つまり、Android システムはクラスをインスタンス化しようとしていますがView、 aContextをそのコンストラクタで)。

public Animations(Context context) {
    super(context);  
}
于 2012-12-12T02:38:02.800 に答える