AlertDialog in AndEngine

我们两清 提交于 2019-12-11 19:56:32

问题


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)

It is pointing to this line

 @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;
  }       

}


回答1:


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).

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.

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.




回答2:


Call showDialog from the UIThread.

this.runOnUiThread(new Runnable() {
    @Override
    public void run() {
        MyActivity.this.showDialog(MY_DIALOG_ID);
    }
});

Oh and NO, NO, NO, NO, NO, NO, NO, NO:

case 1:



回答3:


try this

@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")
              .setIcon(R.drawable.ic_launcher)
              .setPositiveButton("Thanks", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            isGameRunning = true;
            mEngine.start();
            gameStarted = true;

        }
        });
      return builder.create();
  default:
      return null;
  }       
}


来源:https://stackoverflow.com/questions/8997262/alertdialog-in-andengine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!