2013-02-21 2 views
0

내 게임에 문제가 있습니다. 플레이어가 죽을 때마다 "다시 시도?"를 누를 수있는 화면이 나타납니다. 그러나 그가이 버튼을 누르면 게임이 충돌하고 안드로이드는 게임이 종료되었다고 말합니다. 갑자기 게임이 다시 시작됩니다. 게임을 다시 시작하기 전에 충돌 문제를 해결하는 방법을 알 수 없습니다. 모든 배경 작업은 다음과 같습니다 이루어집니다내 코드에서이 SuperNotCalledException을 고치는 방법?

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

} 

그리고 마지막으로 로그 캣 :

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} 

감사합니다 여기에 터치를위한 코드입니다 미리!

답변

1

이처럼 GameActivitysuper.onPause()를 호출해야합니다 :

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

당신은 당신의 com.example.game.GameActivity에 오류가 있습니다. 안드로이드 플랫폼은 응용 프로그램이 올바르게 작동하기 위해서는 수퍼 클래스 (이 경우 Activity)에서 특정 메소드를 호출해야합니다.

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

그러나, 플랫폼 당신이 Activity 클래스에서 기본 onPause() 구현을 호출해야합니다 :

이 오류 메시지는이 같은 Game 활동 onPause() 방법의 재정의가 있음을 알려줍니다. 이렇게하려면 Game.onPause() 방법 본문의 시작 부분에

super.onPause(); 

줄을 입력하십시오. 그래서, 당신은 코드를하게 될 겁니다 :

@Override 
protected void onPause() { 
    super.onPause(); 
    // your code - same as before 
} 
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 
} 
:)을 folloing을
관련 문제