0
votes

I am trying to create a countdown timer within the rendering class which will load the main menu once the countdown has finished.

public void startTimer ()
{
    new CountDownTimer(30000, 1000) {

         public void onTick(long millisUntilFinished) 
         {

         }

         public void onFinish() 
         {
             Toast.makeText(Game.context, "TIMER ENDED", Toast.LENGTH_SHORT).show();
             // put score into DB
             // load up a different screen
         }
      }.start();
}

I tried using the one shown above, however, when I use it my game closes unexpectedly. It is the timer which is causing the game to close (not the toast).

Anyone know a way I can either implement this or fix the example given above?

(The timer function is placed into 'onSurfaceCreated')

Here is the information from the logCat:

12-10 16:41:45.796: E/AndroidRuntime(5336): FATAL EXCEPTION: GLThread 284 12-10 16:41:45.796: E/AndroidRuntime(5336): Process: com.damienrenner.spacefruitshooter, PID: 5336

12-10 16:41:45.796: E/AndroidRuntime(5336): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

12-10 16:41:45.796: E/AndroidRuntime(5336): at android.os.Handler.(Handler.java:200)

12-10 16:41:45.796: E/AndroidRuntime(5336): at android.os.Handler.(Handler.java:114)

12-10 16:41:45.796: E/AndroidRuntime(5336): at android.os.CountDownTimer$1.(CountDownTimer.java:109)

12-10 16:41:45.796: E/AndroidRuntime(5336): at android.os.CountDownTimer.(CountDownTimer.java:109)

12-10 16:41:45.796: E/AndroidRuntime(5336): at com.damienrenner.spacefruitshooter.GRenderer$1.(GRenderer.java:274)

12-10 16:41:45.796: E/AndroidRuntime(5336): at com.damienrenner.spacefruitshooter.GRenderer.startTimer(GRenderer.java:274)

12-10 16:41:45.796: E/AndroidRuntime(5336): at com.damienrenner.spacefruitshooter.GRenderer.onSurfaceCreated(GRenderer.java:331)

12-10 16:41:45.796: E/AndroidRuntime(5336): at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1501)

12-10 16:41:45.796: E/AndroidRuntime(5336): at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1240)

1
Why not just use new Handler().postDelayed ? - cYrixmorten
Pleas provide the crashlog. If i had to guess i would say that you need to instantiate it from the main thread and if i correctly recall onSurfaceCreated is not in the main thread since surfaceview renders on a separate thread - Pasquale Anatriello
I have added the logcat information - RennerStudios

1 Answers

0
votes
public void startTimer ()
{
    new CountDownTimer(30000, 1000) {
        public void onTick(long millisUntilFinished) 
        {

        }

        public void onFinish() 
        {
            runOnUiThread(new Runnable() {
                public void run()
                {
                    Toast.makeText(Game.context, "TIMER ENDED", Toast.LENGTH_SHORT).show();
                    // put score into DB
                    // load up a different screen
                }
            );
        }
    }.start();
}

You want to display a UI message when you aren't in the UI thread. Use the above code to fix that.