Mathieu Gasciolli
Mathieu Gasciolli

Reputation: 138

AlertDialog won't show in my application - why so?

I'm trying to make an AlertDialog when an operation fails. But I can't display it on screen I don't understand why since I did what the tutorial showed.

I know displayDialogError is called because my output shows lol value. But then nothing appears when the AlertDialog is supposed to pop up.

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

In This line, this is my current Activity showing in the screen.

Thanks in advance for helping me.

Upvotes: 1

Views: 74

Answers (3)

Andrey Rankov
Andrey Rankov

Reputation: 2090

You should call create() after you set all dialog parameters. Also, I recommend you to create it just once in the beginning. There is no reason to recreate the same dialog every time this method called.

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

Move alertDialog to a class variable, or pass it to your method as a parameter. And then just show it when needed.

    alertDialog.show();

And be sure you run it on UI thread. If you call this method from some other thread, try this:

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

Upvotes: 3

Khalid Taha
Khalid Taha

Reputation: 3313

Try this:

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

Upvotes: 0

Sunil Soni
Sunil Soni

Reputation: 443

Can you Try the Updated Code There might be context issue

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

Upvotes: 0

Related Questions