0

我是Android開發新手,我正在嘗試創建一個小遊戲。我的第一個CountDownTimerSurfaceViewGameView子類中的構造函數創建。在運行方法內啓動一個新的CountDownTimer

我應該怎樣做才能通過下一個關卡,請撥打CountDownTimer classcancel(),重新獲得下一關的所有元素,並重新創建一個新的CountDownTimer。我在執行RunnableGameView類的run()中執行此操作。

我一直在通過在線閱讀東西,但大多數解決方案都使用Activities,正如我所說的,我在SurfaceView的子類中工作。

這是我收到

E/AndroidRuntime: FATAL EXCEPTION: Thread-4 
       Process: com.example.lucadigiammarino.biogame, PID: 3569 
       java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() 
        at android.os.Handler.<init>(Handler.java:200) 
        at android.os.Handler.<init>(Handler.java:114) 
        at android.os.CountDownTimer$1.<init>(CountDownTimer.java:114) 
        at android.os.CountDownTimer.<init>(CountDownTimer.java:114) 
        at com.example.lucadigiammarino.biogame.GameCountDownTimer.<init>(GameCountDownTimer.java:0) 
        at com.example.lucadigiammarino.biogame.GameView.startTimer(GameView.java:135) 
        at com.example.lucadigiammarino.biogame.GameView.run(GameView.java:306) 
        at java.lang.Thread.run(Thread.java:761) 

預先感謝您的寶貴時間

+0

您嘗試在主線程中運行某些處理UI的函數。 – phnmnn

+0

http://stackoverflow.com/questions/11086129/runonuithread-inside-a-view –

回答

0

感謝@Roman我設法解決我的問題的錯誤,答案就在這裏進行更詳細的解釋 - - >runOnUiThread inside a View

我已經做了什麼我只是在我的run()內使用post()創建了一個不同的Thread。我再說一遍,我在SurfaceView的子類中。

post(new Runnable() { 
    @Override 
    public void run() { 
     myTimer.cancel; 
     startTimer() //creates a new timer 
    } 
}); 

希望它能對未來的其他人有所幫助!

相關問題