AlertDialog.Builder打开另一个AlertDialog.Builder

时间:2012-02-18 17:18:22

标签: android dialog alertdialog

我正在尝试在另一个AlertDialog中打开AlertDialog,但它不起作用,任何想法为什么它不起作用?

String items[] = {"Details","Edit","Delete"}
AlertDialog.Builder alert = new AlertDialog.Builder(getAplicationContext());
alert.setTitle("Options");
alert.setItems(items, new OnClickListener() {

    public void onClick(DialogInterface dialog, int item) {
        switch(item){
            case 0:
                AlertDialog.Builder alert2 = new AlertDialog.Builder(getAplicationContext());
                alert2.setTitle("Details");
                alert2.setMessage(getDetails());
                alert2.setNeutralButton("Close", null);
                alert2.show();
            return;

            case 1:
                //not important for the question
            return;

            case 2:
                //not important for the question
            return;
        }
    }
});

alert.setNegativeButton("Cancel", null);
alert.show();

1 个答案:

答案 0 :(得分:6)

问题可能是您用于AlertDialog的上下文。尝试在两者中使用MyActivityName.this,将MyActivityName替换为Activity的名称。

因此,构建第一个AlertDialog应该如下所示

AlertDialog.Builder alert = new AlertDialog.Builder(MyActivityName.this);

然后

AlertDialog.Builder alert2 = new AlertDialog.Builder(MyActivityName.this);

为第二个。