Android 如何禁用AlertDialog中的按钮?跟进问题

Android 如何禁用AlertDialog中的按钮?跟进问题,android,Android,我昨天问了这个问题(http://stackoverflow.com/questions/7392321/how-do-you-disable-a-button-inside-of-an-alertdialog)并相应地修改了我的代码。。。今天早上,我在模拟器中运行了代码,收到了一个NPE。代码如下: public void monster() { int playerint = settings.getPlayerInt(); int monsterint =

我昨天问了这个问题(http://stackoverflow.com/questions/7392321/how-do-you-disable-a-button-inside-of-an-alertdialog)并相应地修改了我的代码。。。今天早上,我在模拟器中运行了代码,收到了一个NPE。代码如下:

public void monster() {
        int playerint = settings.getPlayerInt();
        int monsterint = settings.getMonsterInt();



        AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
        alertbox.setMessage("You have Encountered a Monster");

        alertbox.setPositiveButton("Fight!",
                new DialogInterface.OnClickListener() {

                    // do something when the button is clicked
                    public void onClick(DialogInterface arg0, int arg1) {
                        createMonster();
                        fight();

                    }
                });

        alertbox.setNeutralButton("Try to Outwit",
                new DialogInterface.OnClickListener() {

                    // do something when the button is clicked
                    public void onClick(DialogInterface arg0, int arg1) {
                        // This should not be static
//                      createTrivia();
                        trivia();

                    }
                });

        // Return to Last Saved CheckPoint
        alertbox.setNegativeButton("Run Away!",
                new DialogInterface.OnClickListener() {

                    // do something when the button is clicked
                    public void onClick(DialogInterface arg0, int arg1) {
//                      runAway();
                    }
                });
问题就是从这里开始的

// show the alert box
            alertbox.show();
            AlertDialog dialog = alertbox.create();
            Button button = dialog.getButton(AlertDialog.BUTTON_NEUTRAL);
            if(monsterint > playerint) {
                button.setEnabled(false);
            }
        }

有人知道我做错了什么吗?

我尝试了这些代码,它工作正常,你需要先隐藏好吗

AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
        alertbox.setMessage("You have Encountered a Monster");
        alertbox.setPositiveButton("asdasd", null);

        alertbox.show();
        alertbox.setPositiveButton("", null);
        alertbox.show();
编辑

起初我认为代码应该是:

        AlertDialog dialog = alertbox.create();
        Button button = dialog.getButton(AlertDialog.BUTTON_NEUTRAL);
        if(monsterint > playerint) {
            button.setEnabled(false);
        }

        dialog.show();
我希望这能起作用,但是不管你如何编写这段代码,对getButton(int)的调用总是返回null

这似乎没有任何合理的理由。我很想说这是API中的一个bug。我的目标是API级别8

更新

恭喜您发现了一个解决方法,请参见注释4

你也可以看看一个可能的

解决方法是在
dialog.show()之后调用getButton


你有两个问题。首先,您应该像这样分别调用
show()
create()
。实际上,您所做的是隐式创建一个AlertDialog,并使用
alertbox.show()
显示它,然后在它的正下方创建第二个
AlertDialog
,用于操作按钮。让我们尽量减少对构建器的直接调用

另外,NPE崩溃的更直接原因是,在
AlertDialog
land中,按钮本身在
AlertDialog
准备好显示之前不会被创建(基本上,在调用
AlertDialog.show()
之后,再次调用
AlertDialog.Builder.show()
方法)。为了使用
AlertDialog
,您需要在显示对话框后获取并操作按钮状态。以下是对最终代码部分的修改,可以解决此问题:

//Create the AlertDialog, and then show it
AlertDialog dialog = alertbox.create();
dialog.show();
//Button is not null after dialog.show()
Button button = dialog.getButton(AlertDialog.BUTTON_NEUTRAL);
if(monsterint > playerint) {
    button.setEnabled(false);
}

HTH

诀窍在于需要使用
AlertDialog.Builder.show()方法返回的
AlertDialog
对象。无需调用
AlertDialog.Builder.create()

例如:

        AlertDialog dialog = alertbox.show();
        if(monsterint > playerint) {
            Button button = dialog.getButton(AlertDialog.BUTTON_NEUTRAL);
            button.setEnabled(false);
        }

@Frank Bozzo我刚刚演示了如何隐藏按钮:(我错了,但是Merlin错了,
button.setVisibility(View.GONE);
@Frank Bozzo我已经更新了我的答案,并给出了一个完整的解释,说明了为什么会发生这种情况,这实际上是不正确的。调用
show()
直接在
AlertDialog.Builder
对象上隐式调用其
create()
方法,然后显示结果。感谢您指出这一点……每天学习新的内容。我已经更新了我的答案代码,但仍在生成新的答案NPE@FrankBozzo我测试了代码…它在我眼前运行…所以你一定错过了什么。注意:正确的代码调用
dialog.getButton(…)
dialog.show()之后。
感谢您的深入回答…不幸的是,即使经过这些修改,我仍然会遇到NPE崩溃!有什么想法吗?
        AlertDialog dialog = alertbox.show();
        if(monsterint > playerint) {
            Button button = dialog.getButton(AlertDialog.BUTTON_NEUTRAL);
            button.setEnabled(false);
        }