How to create alert dialog with andengine

喜欢而已 提交于 2019-12-14 01:56:50

问题


I am developing game using andengine now i need is to create a alert dialog box i am using this

 case MENU_OPT:
        mEngine.runOnUpdateThread(new Runnable() {
         @Override
         public void run() {


             AlertDialog.Builder alert = new AlertDialog.Builder(GameActivity.this);
             alert.setTitle("");
             alert.setMessage("");
             alert.setPositiveButton("OK", new OnClickListener() {
                     @Override
                     public void onClick(DialogInterface arg0, int arg1) {

                     }
             });

             alert.show();
         }
        });
         break;

but getting error java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

what is the problem with code or can i use alertdialog builder with andengine or not.


回答1:


Just make the Object Of Main Activity class and use the Object

activity.runOnUIThread(new Runnable() {
     @Override
     public void run() {


         AlertDialog.Builder alert = new AlertDialog.Builder(GameActivity.this);
         alert.setTitle("");
         alert.setMessage("");
         alert.setPositiveButton("OK", new OnClickListener() {
                 @Override
                 public void onClick(DialogInterface arg0, int arg1) {

                 }
         });

         alert.show();
     }
    });



回答2:


alert.show(); is not the way of showing alert with andengine.

1.You can use Activity.showDialog() for alert.

OR

2.You can use AlertDialog.Builder like:

AlertDialog.Builder builder = new AlertDialog.Builder(this);



回答3:


You have done all the thing right just one change need in your code.

 mEngine.runOnUIThread(new Runnable() {
     @Override
     public void run() {


         AlertDialog.Builder alert = new AlertDialog.Builder(GameActivity.this);
         alert.setTitle("");
         alert.setMessage("");
         alert.setPositiveButton("OK", new OnClickListener() {
                 @Override
                 public void onClick(DialogInterface arg0, int arg1) {

                 }
         });

         alert.show();
     }
    });

You have to UIThread to display dialog not UpdateThread because both have their independent use.



来源:https://stackoverflow.com/questions/17168881/how-to-create-alert-dialog-with-andengine

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