Java – How to create a warning dialog using andengine

How to create a warning dialog using andengine… here is a solution to the problem.

How to create a warning dialog using andengine

I’m using AndEngine to develop games
Now all I need is to create a warning dialog
I’m 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 an error occurred
java.lang.RuntimeException: Cannot create handlers within threads that have not called Looper.prepare().

What’s wrong with the code, or can I use alertdialog builder with andengine.

Solution

Just make an object of the main Activity class and use that 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();
     }
    });

Related Problems and Solutions