问题
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