AlertDialog在AndEngine [英] AlertDialog in AndEngine

查看:204
本文介绍了AlertDialog在AndEngine的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图表现出alertdialog但我不断收到此错误

I am trying to show a alertdialog but i keep getting this error

01-24 21:52:45.640: E/AndroidRuntime(31119): FATAL EXCEPTION: UpdateThread
01-24 21:52:45.640: E/AndroidRuntime(31119): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
01-24 21:52:45.640: E/AndroidRuntime(31119):    at android.os.Handler.<init>(Handler.java:121)
01-24 21:52:45.640: E/AndroidRuntime(31119):    at android.app.Dialog.<init>(Dialog.java:100)
01-24 21:52:45.640: E/AndroidRuntime(31119):    at android.app.AlertDialog.<init>(AlertDialog.java:96)
01-24 21:52:45.640: E/AndroidRuntime(31119):    at android.app.AlertDialog$Builder.create(AlertDialog.java:891)

这是指向该行

 @Override
 protected Dialog onCreateDialog(int id) {
  switch (id) {
  case 1:
      isGameRunning = false;
      gameStarted = false;
      mEngine.stop();
      AlertDialog.Builder builder = new AlertDialog.Builder(this);
      builder.setTitle("How To Play");
      builder.setIcon(R.drawable.ic_launcher);



      final AlertDialog alert = builder.create(); //this line

      builder.setPositiveButton("Thanks", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            alert.dismiss();
            isGameRunning = true;
            mEngine.start();
            gameStarted = true;

        }
        });
      return alert;
  default:
      return null;
  }       

}

推荐答案

这似乎是出于某种原因,该方法 onCreateDialog 从AndEngine的UpdateThread(虽然叫它应该从UI线程调用)。

It seems like, for some reason, the method onCreateDialog is called from the UpdateThread of AndEngine (While it should be called from the UI Thread).

请记住,当你看一个对话框,你叫的ShowDialog(INT ID),而不是 onCreateDialog(INT ID)。之后您调用的ShowDialog ,Android将调用 onCreateDialog 在UI线程时可能的。

Remember that when you show a dialog, you call showDialog(int id) rather than onCreateDialog(int id). After you call showDialog, Android will call onCreateDialog on the UI thread when possible.

其实,我看着的ShowDialog 信息现在,这是德precated,它看起来像有对话创造一种全新的方式。阅读更多 rel=\"nofollow\">。

In fact, I looked in showDialog info now and it is deprecated, it looks like there is a whole new way of creating dialogs. Read more here.

这篇关于AlertDialog在AndEngine的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆