倒计时器导致Android游戏应用程序意外关闭

时间:2013-12-11 11:12:37

标签: android opengl-es

我正在尝试向我的渲染类添加倒计时器,但是,当我添加startTimer函数并运行游戏时,游戏关闭并说出了意外错误。由于我已将问题分配给计时器功能,因此我已从此类中省略了大量代码。

public class GRenderer implements Renderer 
{
    // I have omitted a lot of irrelevant code for this question 

    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 a different screen
             }
          }.start();
    }

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

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) 
    {
        // Omitted functions
    }

    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) 
    {
        startTimer();
        // Omitted functions
    }

}

这是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 个答案:

答案 0 :(得分:1)

你是从工作线程调用的。您可以使用处理程序,或运行您的代码,如: -

以下是您可以用来解决问题的方法: -

runOnUiThread

new Thread()
{
    public void run()
    {
        myactivity.this.runOnUiThread(new runnable()
        {
            public void run()
            {
                //Do your UI operations like dialog opening or Toast here
            }
        });
    }
}.start();

LOOPER

class LooperThread extends Thread {
    public Handler mHandler;

    public void run() {
        Looper.prepare();

        mHandler = new Handler() {
            public void handleMessage(Message msg) {
                // process incoming messages here
            }
        };

        Looper.loop();
    }
}

的AsyncTask

public void onClick(View v) {
    new CustomTask().execute((Void[])null);
}


private class CustomTask extends AsyncTask<Void, Void, Void> {

    protected Void doInBackground(Void... param) {
        //Do some work
        return null;
    }

    protected void onPostExecute(Void param) {
        //Print Toast or open dialog
    }
}

处理程序

    Message msg = new Message();


    new Thread()
    {
        public void run()
        {
            msg.arg1=1;
            handler.sendMessage(msg);
        }
    }.start();


Handler handler = new Handler(new Handler.Callback() {

    @Override
    public boolean handleMessage(Message msg) {
        if(msg.arg1==1)
        {
            //Print Toast or open dialog        
        }
        return false;
    }
});