0

ゲームに問題があります。プレイヤーが死亡するたびに、「Try Again?」を押すことができる画面が表示されます。しかし、彼がこのボタンを押すと、ゲームがクラッシュし、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);

            }
        }

すべてのバックグラウンド作業が行われる My Panel は、次のようになります。

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 ActivityActivityAndroid プラットフォームでは、アプリケーションが正しく動作するために、スーパークラス (この場合) から特定のメソッドを呼び出す必要があります。

このエラー メッセージは、アクティビティに次のような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アクティビティに移動し、onPause ()で次のことを行います。

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