0

アクティビティの onPostResume メソッドからアラート ダイアログを作成して表示します。ダイアログは表示されませんが、理由がわかりません。

ダイアログを表示するための私のコード:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("message");
builder.setPositiveButton("a", aListener);
builder.setPositiveButton("b", bListener);
builder.setCancelable(false);

AlertDialog dlg = builder.create();
dlg.show();
4

1 に答える 1

1

使用してみてください:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("message");
builder.setPositiveButton("a", aListener);
builder.setPositiveButton("b", bListener);
builder.setCancelable(false);
builder.show();

注:AlertDialog別のインスタンスを作成する理由はありません。


または、 new を返すメソッドを作成できる別の正しいアプローチAlertDialog:

protected static final int CREATE_INFORMATION_DIALOG = 1320;

private Dialog createDialog(int type) {
        AlertDialog dialog = null;
        switch (type) {
            case CREATE_INFORMATION_DIALOG:
                dialog = new AlertDialog.Builder(this)
                    .setTitle("Information")
                    .setMessage("Download was finished successfully.")
                    .setPositiveButton("Close", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dlg, int whichButton) {

                        }
                    })
                    .create();
                break;
        }
        return dialog;
    }

そして、それを次のように呼び出します

createDialog(CREATE_INFORMATION_DIALOG).show();
于 2012-07-22T13:33:15.343 に答える