开发者

Problem with SurfaceHolder.Callback Interface in Android SDK

I've been tinkering with the idea of delving into the Android SDK since I got my Droid X a while ago, and just recently acted on it. I installed the ADT plugin in Eclipse, downloaded and installed the Android SDK (Rev. 11), and tried some stuff out. I wanted to try 2d graphics out, since they're a huge departure from what I spend my hours on at work (embedded systems development).

I've tried to follow the tutorial here, but can't build due to some errors with two interfaces that are implemented. I'm trying to build for 2.3.3 on a 64 bit Windows 7 machine. I'll include the code below, with the important caveat that I'm not a typically Java programmer and something obvious could easily have escaped me.

Drawable Panel.java

package com.android.tutorial;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public abstract class DrawablePanel 
    extends SurfaceView 
    implements SurfaceHolder.Callback, ISurface {

    private AnimationThread thread;

    public DrawablePanel(Context context) {
        super(context);
        getHolder().addCallback(this);

        this.thread = new AnimationThread(getHolder(), this);
    }

     @Override
     public void onDraw(Canvas canvas) {
     }

     @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        thread.setRunning(true);
        thread.start();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        boolean retry = true;
        thread.setRunning(false);
        while (retry) {
            try {
                thread.join();
                retry = false;
            } catch (InterruptedException e) {
                // we will try it again and again...
            }
        }           
    }
}

ISurface.java

package com.android.tutorial;

import android.graphics.Canvas;

public interface ISurface {
    void onInitialize();
    void onDraw(Canvas canvas);
    void onUpdate(long gameTime);
}

AndroidTutorial.java

package com.android.tutorial;


import android.app.Activity;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphi开发者_如何学Pythoncs.Canvas;
import android.os.Bundle;

public class AndroidTutorial extends Activity {
    AnimatedSprite animation = new AnimatedSprite();

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new AndroidTutorialPanel(this));
    }

    class AndroidTutorialPanel extends DrawablePanel {

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

        @Override
        public void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            AndroidTutorial.this.animation.draw(canvas);
        }

        @Override
        public void onInitialize() {
            AndroidTutorial.this.animation.Initialize(
                    BitmapFactory.decodeResource(
                            getResources(), 
                            R.drawable.explosion), 
                            32, 32, 14, 7);
        }

        @Override
        public void onUpdate(long gameTime) {
            AndroidTutorial.this.animation.Update(gameTime);
        }
    }

}

I'm getting the following error in DrawablePanel.java at the definitions of surfaceChanged(), surfaceCreated(), and surfaceDestroyed():

The method [name of function mentioned above](SurfaceHolder) of type DrawablePanel must override a superclass method.

It's the same thing for onInitialize and onUpdate in AndroidTutorial.java. I'm lost since I can copy/paste the same thing at work (ver 10 of the SDK), with no errors. Any thoughts?


I realize this is a bit late, and hopefully you have figured it out, but you are implementing SurfaceHolder.Callback not extending it.
Remove the @Override from the functions and it should work.


This may be a bit old, and you probably figured it out, but I had the same problem (I was following the same example as you). Turns out that these compilation errors are because of the Java Compiler level that you used, you have to set it on 1.6 (so it takes the @Override annotation as valid for interface implemented methods as well), if you use level 1.5, it is alright to remove the @Override annotation too.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜