Android 如何在Andengine中显示alertDialog?

Android 如何在Andengine中显示alertDialog?,android,andengine,Android,Andengine,我正在使用AndEngine进行我的游戏。因为我想让用户在单击图像时选择,所以应该会弹出一个警告对话框。我的代码如下。 这里是方法 @Override protected Dialog onCreateDialog(int id) { switch (id) { case 1: AlertDialog.Builder builder = new AlertDialog.Builder(MyGame.this.getApplicationContext());

我正在使用AndEngine进行我的游戏。因为我想让用户在单击图像时选择,所以应该会弹出一个警告对话框。我的代码如下。 这里是方法

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
    case 1:
        AlertDialog.Builder builder = new AlertDialog.Builder(MyGame.this.getApplicationContext());
        builder.setMessage("Hello");
        AlertDialog alert = builder.create();
        return alert;
    default:
        return null;
    }       
}
我通过以下语句调用此方法:

{
onCreateDialog(1).show();
}

我收到这个错误信息:

E/AndroidRuntime(672): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
onCreateDialog(1.show()是错误的。
调用
showDialog(1)取而代之

并按如下方式更改生成器:

AlertDialog.Builder builder = new AlertDialog.Builder(MyGame.this);
不要使用onCreateDialog(1.show()

使用

反而

并将MyGame.this.getApplicationContext()替换为MyGame.this

AlertDialog.Builder builder = new AlertDialog.Builder(MyGame.this);
showDialog(1)