0

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

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}

先感谢您!

4

3 回答 3

1

确保像这样打电话给super.onPause()Game Activity

protected void onPause() {
    // your cleanup code
    super.onPause();
}
于 2013-02-21T12:05:46.230 回答
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
}
于 2013-02-21T12:06:19.413 回答
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 Activity 并在您的onPause()中执行以下操作:

protected void onPause(){
   super.onPause();
// the rest of your code
}
于 2013-02-21T12:06:44.790 回答