本文介绍了你如何禁用AlertDialog内部的按钮?跟进质询,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我问这个问题,昨日(http://stackoverflow.com/questions/7392321/how-do-you-disable-a-button-inside-of-an-alertdialog),并相应修改了code。 ..今天早上我跑了code在模拟器并获得了NPE。这里是code:

I asked this question yesterday (http://stackoverflow.com/questions/7392321/how-do-you-disable-a-button-inside-of-an-alertdialog) and modified my code accordingly... this morning I ran the code in an emulator and received an NPE. Here is the code:

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

这是那里的问题就开始

THIS IS WHERE THE PROBLEM STARTS

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

任何人都知道我在做什么错了?

Anyone know what I am doing wrong?

推荐答案

您有两个问题。首先,你应该叫显示()创建()单独那样。什么你实际上做的是隐式创建一个AlertDialog和 alertbox.show显示它(),然后用鼠标右键下方创建第二个 AlertDialog 您使用操作按钮。让我们尽量保持直接调用生成器在最低限度。

You have two problems. The first is that you should be calling show() and create() separately like that. What you've actually done is implicitly create one AlertDialog and display it with alertbox.show(), and then right below it create a second AlertDialog that you are using to manipulate the button. Let's try to keep the direct calls to the Builder at a minimum.

此外,在 AlertDialog 更直接的原因,您的NPE崩溃,登陆按钮本身实际上并没有获得创建,直到 AlertDialog 是ppared显示($ P $基本上,在 AlertDialog.show()叫......再次,不能用<$困惑C $ C> AlertDialog.Builder.show()法)。为了使用 AlertDialog 你的目的,你需要获取并显示该对话框后,操纵按钮的状态。这是你的最后code部分的修正版,修复了这一点:

Also, and the more direct reason for your NPE crash, in AlertDialog land the buttons themselves don't actually get created until the AlertDialog is prepared for display (basically, after AlertDialog.show() is called...again, not to be confused with the AlertDialog.Builder.show() method). In order to use AlertDialog for your purposes, you need to obtain and manipulate the button state after displaying the dialog. Here is a modification of your final code section that fixes this:

//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);
}

心连心

这篇关于你如何禁用AlertDialog内部的按钮?跟进质询,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 19:47