1

I am trying to do a toast from my rendering class.. What can I do to make it work? Here is my code (ive omitted irrelevant chunks) (My activity is called Game)

public class GRenderer extends Game implements Renderer 
{

    public void timer ()
     {
        Game.currentGameTime = System.currentTimeMillis();
        Game.ellapsedTime = Game.currentGameTime - Game.gameStartTime;

        if (Game.ellapsedTime > 10000)
        {
            // THIS IS WHEN The GAME SHOULD END
            Toast.makeText(Game.context, "10Seconds", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onDrawFrame(GL10 gl) // when drawing to screen
    {
        timer();
    }
}

Here is my logcat:

12-13 17:14:59.856: E/AndroidRuntime(17521): FATAL EXCEPTION: GLThread 1323

12-13 17:14:59.856: E/AndroidRuntime(17521): Process: com.damienrenner.spacefruitshooter, PID: 17521

12-13 17:14:59.856: E/AndroidRuntime(17521): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

12-13 17:14:59.856: E/AndroidRuntime(17521):    at android.os.Handler.<init>(Handler.java:200)

12-13 17:14:59.856: E/AndroidRuntime(17521):    at android.os.Handler.<init>(Handler.java:114)

12-13 17:14:59.856: E/AndroidRuntime(17521):    at android.widget.Toast$TN.<init>(Toast.java:327)

12-13 17:14:59.856: E/AndroidRuntime(17521):    at android.widget.Toast.<init>(Toast.java:92)

12-13 17:14:59.856: E/AndroidRuntime(17521):    at android.widget.Toast.makeText(Toast.java:241)

12-13 17:14:59.856: E/AndroidRuntime(17521):    at com.damienrenner.spacefruitshooter.GRenderer.timer(GRenderer.java:278)

12-13 17:14:59.856: E/AndroidRuntime(17521):    at com.damienrenner.spacefruitshooter.GRenderer.onDrawFrame(GRenderer.java:291)

12-13 17:14:59.856: E/AndroidRuntime(17521):    at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1523)

12-13 17:14:59.856: E/AndroidRuntime(17521):    at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1240)

Thank you!

Logcat after Ken Wolf's implementation:

12-13 18:00:14.152: E/AndroidRuntime(18966): FATAL EXCEPTION: GLThread 1365

12-13 18:00:14.152: E/AndroidRuntime(18966): Process: com.damienrenner.spacefruitshooter, PID: 18966

12-13 18:00:14.152: E/AndroidRuntime(18966): java.lang.ClassCastException: android.app.Application cannot be cast to android.app.Activity

12-13 18:00:14.152: E/AndroidRuntime(18966):    at com.damienrenner.spacefruitshooter.GRenderer.timer(GRenderer.java:279)

12-13 18:00:14.152: E/AndroidRuntime(18966):    at com.damienrenner.spacefruitshooter.GRenderer.onDrawFrame(GRenderer.java:300)

12-13 18:00:14.152: E/AndroidRuntime(18966):    at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1523)

12-13 18:00:14.152: E/AndroidRuntime(18966):    at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1240)

12-13 18:00:16.204: I/Process(18966): Sending signal. PID: 18966 SIG: 9

1 Answers1

2

Toasts must be called on the UI thread.

Assuming Game.context holds a reference to the calling Activity, change your code to something like this:

((Activity) Game.context).runOnUiThread(new Runnable() {
    @Override
    public void run() {
        Toast.makeText(Game.context, "10Seconds", Toast.LENGTH_SHORT).show();                   
    }
});

More info here: Android: Toast in a thread

http://developer.android.com/reference/android/app/Activity.html#runOnUiThread(java.lang.Runnable)

Community
  • 1
  • 1
Ken Wolf
  • 23,133
  • 6
  • 63
  • 84
  • I have implemented this, however, the game is still being force quit ('Unfortunately, Game has stopped') – RennerStudios Dec 13 '13 at 17:47
  • No, I have added the logcat to the question – RennerStudios Dec 13 '13 at 18:02
  • Well, my assumption that `Game.context` was an `Activity` was wrong. Pass your calling activity into your `GRenderer` or `Game` and call `runOnUiThread` on that. – Ken Wolf Dec 13 '13 at 18:04
  • Sorry I dont understand ... this is how I call the Game Activity ... Intent game = new Intent(getApplicationContext(),Game.class); – RennerStudios Dec 13 '13 at 18:11
  • You need to pass a reference to the Game Activity to your `GRenderer` class and call `runOnUiThread` on that. Presumably `GRenderer` is insantiated from your `Activity` somewhere? – Ken Wolf Dec 13 '13 at 18:13