如何在我的代码中修复此SuperNotCalledException?

时间:2013-02-21 12:02:36

标签: android surfaceholder

我的游戏有问题。每次玩家死亡时,都会出现一个屏幕,可以按“再试一次?”。然而当他按下这个按钮时,游戏崩溃并且安卓表示游戏已经终止。然后突然重启游戏。在重新启动之前,我无法弄清楚如何解决游戏崩溃的问题。这是触摸的代码:

if (game_finish == 1) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {

                System.out.println("Trying again!!");
                activity.finish();

                Intent myIntent = new Intent(getContext(), Game.class);
                getContext().startActivity(myIntent);

            }
        }

我的面板,完成所有后台工作,如下所示:

public abstract class Panel extends SurfaceView implements
        SurfaceHolder.Callback {

    public Panel(Context context) {
        super(context);

        getHolder().addCallback(this);

        runner = new RunnerThread(getHolder(), this);

        setFocusable(true);

        textPaint = new Paint();
        textPaint.setColor(Color.BLUE);
        textPaint.setTextSize(100);
        textPaint.setFakeBoldText(true);

        textPaint_1 = new Paint();
        textPaint_1.setColor(Color.BLUE);
        textPaint_1.setTextSize(50);
        textPaint_1.setFakeBoldText(true);

    }


    protected void update() {
    }


    protected void drawPrimitives(Canvas canvas) {

    }


    protected boolean processTouch(MotionEvent event) {
        return true;
    }


    public boolean addSprite(Sprite sprite) {

        synchronized (runner.getSurfaceHolder()) {
            return sprites.add(sprite);
        }
    }

    @Override
    public void onDraw(Canvas canvas) {


        canvas.drawColor(Color.BLACK);

        // draw all the panel's sprites
        for (Sprite s : sprites) {

            Rect destRect = new Rect(s.getLocation().x, s.getLocation().y,
                    s.getLocation().x + s.getSpriteWidth(), s.getLocation().y
                            + s.getSpriteHeight());

            canvas.save(); // save the position of the canvas
            if (s.getRotation() < 0 | s.getRotation() > 0) {
                canvas.rotate(s.getRotation(),
                        s.getLocation().x + (s.getSpriteWidth() / 2),
                        s.getLocation().y + (s.getSpriteHeight() / 2)); 
                canvas.drawBitmap(s.getGraphic(), s.getSourceRect(), destRect,
                        null); 
            }

            else {
                canvas.drawBitmap(s.getGraphic(), s.getSourceRect(), destRect,
                        null);
            }
            canvas.restore();

        }


        drawPrimitives(canvas);

        if (World.getGame_finish() == 1) {

            paint.setARGB(128, 255, 255, 255);

            rectF.set(0, 0, getMeasuredWidth(), getMeasuredHeight());
            canvas.drawRect(rectF, paint);
            canvas.drawText("GAME OVER", getWidth() / 4, getHeight() / 2,
                    textPaint);
            canvas.drawText("Try Again?", getWidth() / 5,
                    getHeight() / 2 + 150, textPaint_1);
            canvas.drawText("Return to Menu", getWidth() / 2,
                    getHeight() / 2 + 150, textPaint_1);
        }

        if (World.getPaused() == 1) {

            paint.setARGB(128, 255, 255, 255);

            rectF.set(0, 0, getMeasuredWidth(), getMeasuredHeight());
            canvas.drawRect(rectF, paint);
            canvas.drawText("PAUSED", getWidth() / 3, getHeight() / 2,
                    textPaint);
            canvas.drawText("Press anywhere to continue", getWidth() / 4,
                    getHeight() / 2 + 150, textPaint_1);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return processTouch(event);
    }


    public void surfaceCreated(SurfaceHolder holder) {

        if (programRunning)
            runner.onResume();
        else {
            runner.setRunning(true);
            runner.start();
            programRunning = true;
        }
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        if (programRunning)
            runner.onPause();
        else {

            boolean retry = true;

            runner.setRunning(false);

            while (retry) {
                try {
                    runner.join();
                    retry = false;
                } catch (InterruptedException e) {
                }
            }
        }
    }

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

    // thread to update this panel
    protected static RunnerThread runner;
    // list of all sprites on the panel
    private ArrayList<Sprite> sprites = new ArrayList<Sprite>();
    public boolean programRunning = false;
    private Paint textPaint;
    private Paint textPaint_1;
    final RectF rectF = new RectF();
    final Paint paint = new Paint();
}

class RunnerThread extends Thread {


    public RunnerThread(SurfaceHolder holder, Panel panel) {
        this.holder = holder;
        this.panel = panel;
    }

    protected void setRunning(boolean running) {
        this.running = running;
    }

    protected SurfaceHolder getSurfaceHolder() {
        return holder;
    }

    protected void onPause() {

        paused = true;

    }

    protected void onResume() {
        // monitor pauseLock
        synchronized (pauseLock) {
            paused = false;

            pauseLock.notifyAll();
        }
    }

    // the main loop of program
    @Override
    public void run() {
        Canvas canvas;

        while (running) {
            canvas = null;

            try {
                // monitor pauseLock
                synchronized (pauseLock) {
                    while (paused) {
                        try {

                            pauseLock.wait();
                        } catch (InterruptedException e) {
                        }
                    }
                }



                canvas = holder.lockCanvas();
                synchronized (holder) {
                    // update game
                    panel.update();
                    // draw the surface
                    panel.onDraw(canvas);
                }

            } finally {
                if (canvas != null) {

                    holder.unlockCanvasAndPost(canvas);
                }
            }
        }
    }

    private SurfaceHolder holder;
    private Panel panel;
    private boolean running = false;
    public boolean paused = false;
    private final Object pauseLock = new Object();

}

最后是logcat:

02-21 12:40:54.315: D/dalvikvm(28799): GC_CONCURRENT freed 532K, 6% free 9481K/10048K, paused 3ms+4ms, total 34ms
02-21 12:40:54.456: D/Finsky(28799): [1] 5.onFinished: Installation state replication succeeded.
02-21 12:40:56.096: I/System.out(29399): Trying again!!
02-21 12:40:56.143: D/AndroidRuntime(29399): Shutting down VM
02-21 12:40:56.143: W/dalvikvm(29399): threadid=1: thread exiting with uncaught exception (group=0x40d06930)
02-21 12:40:56.143: I/ActivityManager(390): START u0 {cmp=com.example.game/.Game} from pid 29399
02-21 12:40:56.143: E/AndroidRuntime(29399): FATAL EXCEPTION: main
02-21 12:40:56.143: E/AndroidRuntime(29399): android.app.SuperNotCalledException: Activity {com.example.game/com.example.game.Game} did not call through to super.onPause()
02-21 12:40:56.143: E/AndroidRuntime(29399):    at android.app.Activity.performPause(Activity.java:5210)
02-21 12:40:56.143: E/AndroidRuntime(29399):    at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1226)
02-21 12:40:56.143: E/AndroidRuntime(29399):    at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3002)
02-21 12:40:56.143: E/AndroidRuntime(29399):    at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2971)
02-21 12:40:56.143: E/AndroidRuntime(29399):    at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:2949)
02-21 12:40:56.143: E/AndroidRuntime(29399):    at android.app.ActivityThread.access$800(ActivityThread.java:141)
02-21 12:40:56.143: E/AndroidRuntime(29399):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1251)
02-21 12:40:56.143: E/AndroidRuntime(29399):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-21 12:40:56.143: E/AndroidRuntime(29399):    at android.os.Looper.loop(Looper.java:137)
02-21 12:40:56.143: E/AndroidRuntime(29399):    at android.app.ActivityThread.main(ActivityThread.java:5041)
02-21 12:40:56.143: E/AndroidRuntime(29399):    at java.lang.reflect.Method.invokeNative(Native Method)
02-21 12:40:56.143: E/AndroidRuntime(29399):    at java.lang.reflect.Method.invoke(Method.java:511)
02-21 12:40:56.143: E/AndroidRuntime(29399):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-21 12:40:56.143: E/AndroidRuntime(29399):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-21 12:40:56.143: E/AndroidRuntime(29399):    at dalvik.system.NativeStart.main(Native Method)
02-21 12:40:56.643: W/ActivityManager(390): Activity pause timeout for ActivityRecord{4179a378 u0 com.example.game/.Game}
02-21 12:40:57.690: I/Process(29399): Sending signal. PID: 29399 SIG: 9
02-21 12:40:57.714: I/ActivityManager(390): Process com.example.sperm_beta (pid 29399) has died.
02-21 12:40:57.714: I/WindowState(390): WIN DEATH: Window{41e37e08 u0 com.example.game/com.example.game.Game}
02-21 12:40:57.714: I/WindowState(390): WIN DEATH: Window{41e2e4e0 u0 com.example.game/com.example.game.Menu}
02-21 12:40:57.714: I/WindowState(390): WIN DEATH: Window{41e57898 u0 SurfaceView}
02-21 12:40:57.729: D/dalvikvm(29419): Late-enabling CheckJNI
02-21 12:40:57.729: I/ActivityManager(390): Start proc com.example.game for activity com.example.game/.Game: pid=29419 uid=10119 gids={50119, 1028}

提前谢谢!

3 个答案:

答案 0 :(得分:1)

请务必在super.onPause() Game中拨打Activity,如下所示:

protected void onPause() {
    // your cleanup code
    super.onPause();
}

答案 1 :(得分:0)

com.example.game.Game Activity出现错误。 Android平台要求您从超类(在本例中为Activity)中调用某些方法,以使应用程序正常运行。

此错误消息告诉您在onPause()活动中覆盖了Game方法,如下所示:

@Override
protected void onPause() {
    // your code
}

但是,该平台要求您从onPause()类调用默认的Activity实现。为此,请填写以下行:

super.onPause();

Game.onPause()方法正文的开头。所以,你最终会得到代码:

@Override
protected void onPause() {
    super.onPause();
    // your code - same as before
}

答案 2 :(得分:0)

错误消息显示:

02-21 12:40:56.143: E/AndroidRuntime(29399): android.app.SuperNotCalledException: Activity {com.example.game/com.example.game.Game} did not call through to super.onPause()

请转到您的 com.example.game.Game 活动,然后在 onPause()中执行以下操作:

protected void onPause(){
   super.onPause();
// the rest of your code
}