0

I have a problem with my game. Everytime the player dies, a screen appears where one can press "Try Again?". However when he presses this button the game crashes and android says the game was terminated. And then suddenly the game restarts. I can't figure out how to fix the problem with the game crashing before restarting. Here is the code for the touch:

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);

            }
        }

My Panel, where all the background work is done looks like this:

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();

}

And finally the 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}

Thank you in advance!

3 Answers3

1

Make sure to call super.onPause() in your Game Activity like this:

protected void onPause() {
    // your cleanup code
    super.onPause();
}
M-Wajeeh
  • 17,204
  • 10
  • 66
  • 103
0

You have an error in your com.example.game.Game Activity. Android platform requires you to call certain methods from the superclass (Activity in this case) in order for the application to behave correctly.

This error message tells you that you have an override of onPause() method in the Game activity like this:

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

However, the platform requires that you call the default onPause() implementation from the Activity class. To do this, put the line:

super.onPause();

in the beginning of your Game.onPause() method body. So, you'll end up with the code:

@Override
protected void onPause() {
    super.onPause();
    // your code - same as before
}
andr
  • 15,970
  • 10
  • 45
  • 59
0

The error message says:

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()

So go to your com.example.game.Game Activity and in your onPause() do the folloing:

protected void onPause(){
   super.onPause();
// the rest of your code
}
Mr.Me
  • 9,192
  • 5
  • 39
  • 51