【问题标题】:AlertDialog won't show in my application - why so?AlertDialog 不会显示在我的应用程序中 - 为什么会这样?
【发布时间】:2019-01-16 13:48:36
【问题描述】:

我正在尝试在操作失败时创建一个 AlertDialog。但是我无法在屏幕上显示它我不明白为什么,因为我做了教程显示的内容。

我知道 displayDialogError 被调用是因为我的输出显示了 lol 值。 但是当 AlertDialog 应该弹出时,什么都没有出现。

public String lol;

public void doThings(String str) {
    lol = str;
    if (!lol.isEmpty()) {
        System.out.println(lol);
        displayDialogError();
    }
    else
        System.out.println("Request worked");
}

public void displayDialogError() {
    AlertDialog alertDialog = new AlertDialog.Builder(this).create();
    alertDialog.setTitle("Alert");
    alertDialog.setMessage("Alert message to be shown");
    alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
    alertDialog.show();
}

AlertDialog alertDialog = new AlertDialog.Builder(this).create();

在这一行中,this 是我当前在屏幕上显示的 Activity。

提前感谢您帮助我。

【问题讨论】:

  • 此代码是否在您的活动类中?您是否进行了调试以检查是否调用了 displayDialogError()
  • 是的,它被调用了,是的,它在我的活动课程中:)

标签: android android-alertdialog


【解决方案1】:

您应该在设置所有对话框参数后调用create()。另外,我建议您在开始时只创建一次。没有理由每次调用此方法时都重新创建相同的对话框。

    AlertDialog.Builder builder = new AlertDialog.Builder(<YourActivity>.this);
    builder.setTitle("Alert");
    builder.setMessage("Alert message to be shown");
    builder.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });

    AlertDialog alertDialog = builder.create();

alertDialog 移动到类变量中,或将其作为参数传递给您的方法。然后在需要时显示它。

    alertDialog.show();

并确保在 UI 线程上运行它。如果你从其他线程调用这个方法,试试这个:

runOnUiThread(new Runnable() {
            @Override
            public void run() {
                alertDialog.show();
            }
        }

【讨论】:

    【解决方案2】:

    你能试试更新的代码吗可能存在上下文问题

     public void displayDialogError() {
        AlertDialog alertDialog = new AlertDialog.Builder(YourActivity.this);
        alertDialog.setTitle("Alert");
        alertDialog.setMessage("Alert message to be shown");
        alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
        alertDialog.show();
    }
    

    【讨论】:

    • 嗨!什么都没有改变,我仍然什么都没有:(也许我的活动是全屏的事实改变了什么?
    【解决方案3】:

    试试这个:

    new AlertDialog.Builder(YourActivity.this).setTitle("Alert")
        .setMessage("Alert message to be shown")
        .setPositiveButton("OK",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            }).show();
    

    【讨论】:

    • 无法编译:错误:找不到符号方法 setButton(int,String,)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-27
    • 2016-08-11
    • 1970-01-01
    • 2019-12-03
    • 1970-01-01
    • 1970-01-01
    • 2014-11-02
    相关资源
    最近更新 更多