Java 在视图中创建简单的是/否对话框(Android)

Java 在视图中创建简单的是/否对话框(Android),java,android,android-activity,Java,Android,Android Activity,我的活动中有一个onCreateDialog方法,它有一个case开关,可以根据请求显示不同的对话框 我无法从视图中使用showDialog()方法,因为无法从创建视图时传递的上下文中访问它。至少,我找不到访问它的方法 如何从应用程序的视图使用showDialog?我需要创建一个侦听器吗?如果是,怎么做?有更好的方法吗 以下是应用程序活动中存在的onCreateDialog代码: protected Dialog onCreateDialog(int id) { AlertDialog

我的活动中有一个onCreateDialog方法,它有一个case开关,可以根据请求显示不同的对话框

我无法从视图中使用showDialog()方法,因为无法从创建视图时传递的上下文中访问它。至少,我找不到访问它的方法

如何从应用程序的视图使用showDialog?我需要创建一个侦听器吗?如果是,怎么做?有更好的方法吗

以下是应用程序活动中存在的onCreateDialog代码:

protected Dialog onCreateDialog(int id) {
    AlertDialog alert=null;
    switch(id) {
    case DIALOG_GAMEOVER_ID:
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("You died. Play again?")
               .setCancelable(false)
               .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       //init();
                       //oGameState = eGameState.PLAYING; 
                      dialog.cancel();
                   }
               })
               .setNegativeButton("No", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       finish();
                   }
               });
            alert = builder.create();
        break;
    default:
        break;
    }

    return alert;
}
    // set our MainView as the View
    oNebulaMainView = new NebulaMainView(this, this);
    setContentView(oNebulaMainView);
我试图传递对我的活动的引用,结果崩溃了。也许我做错了

在我的活动中:

protected Dialog onCreateDialog(int id) {
    AlertDialog alert=null;
    switch(id) {
    case DIALOG_GAMEOVER_ID:
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("You died. Play again?")
               .setCancelable(false)
               .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       //init();
                       //oGameState = eGameState.PLAYING; 
                      dialog.cancel();
                   }
               })
               .setNegativeButton("No", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       finish();
                   }
               });
            alert = builder.create();
        break;
    default:
        break;
    }

    return alert;
}
    // set our MainView as the View
    oNebulaMainView = new NebulaMainView(this, this);
    setContentView(oNebulaMainView);
我认为:

public NebulaMainView(Context context, Activity oActivity) {
    super(context);
    // adding the callback (this) to the surface holder to intercept events
    getHolder().addCallback(this);

    // create the game loop thread
    thread = new NebulaMainThread(getHolder(), this);

    setFocusable(true);

    oActivity.showDialog(DIALOG_GAMEOVER_ID);
}

将alert声明为实例变量

我可能在这里遗漏了一些内容,但是什么阻止了您仅仅调用:

alert.show()


只需让视图可以访问警报,并在视图中调用该表单。

我正在使用谷歌在文档中概述的做法。如果我要提醒.show(),我不相信它与活动有关。引用文档:“对话框始终作为活动的一部分创建和显示。通常,您应该从活动的onCreateDialog(int)回调方法中创建对话框。”然后,当您使用showDialog(int)时,将调用onCreateDialog。从资源加载对话框时,似乎会使用showDialog()。本例中的对话框是以编程方式创建的。如果您注意到,当您调用AlertDialog.Builder(上下文)时,您正在将活动传递到警报对话框;文档中没有提到showDialog()用于从资源加载。Salx,我的错。我想我现在明白了。我想您可能会尝试在活动启动时保存对它的引用,以便让视图对其调用showDialog()。我确信,即使您使用alert.show(),它仍然附加到活动。不过,你参考的教程看起来确实更清晰。我认为这不管用,因为这个应用程序是多线程的。活动中的oncreatedialog方法需要处理对话框的创建。或者我必须找到某种方法在UI线程上异步处理这个问题。(见下面我对凯文的评论..类似)你能更详细地解释一下吗?如果我将alert声明为实例变量,如何从视图中访问它?是的,我可以在创建视图时传递对它的引用。但是,这些对话框需要通过onCreateDialog方法在UI线程上创建。这是一个多线程应用程序,我不认为我可以在任何地方弹出对话框。活动应该处理它。NebulaMainView是否从视图继承?异常日志显示错误是什么?