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.
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();
}
});
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);
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.