Android - AlertDialog doesn't show when called from Main-Method

Deadly 提交于 2019-12-11 18:27:52

问题


I've got a problem with an AlertDialog. The code works well, when I put it in a onClick-Listener of a Button, but it doesn't work at all when I put it at the end of my main-method.

This is the Method that shows the AlertDialog:

void showMaths(){
    AlertDialog.Builder alert = new AlertDialog.Builder(LabyRiddle.this);

    alert.setTitle("Title");
    alert.setMessage("Message");

    // Set an EditText view to get user input 
    final EditText input = new EditText(LabyRiddle.this);
    alert.setView(input);

    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
      }
    });

    alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int whichButton) {
      }
    });

    alert.create();  // <== Doesn't make any difference whether its here or not
    alert.show();
}

I just want that the main-method runs and at the end of it, the alert appears...

But it doesn't appear, and it doesn't crash or even give an error report.

Or is there another possibility to show the alert after the main method has finished?

Thanks, and have a nice day!

Oliver


回答1:


I think you need to call

alert.create().show();

Before you call show();, you must create your dialog from builder with create(); method




回答2:


You are forget to call this line

 alert.create();

before

alert.show();

For More Information About Alert Dialog Refer this Link http://developer.android.com/guide/topics/ui/dialogs.html#AlertDialog



来源:https://stackoverflow.com/questions/14444064/android-alertdialog-doesnt-show-when-called-from-main-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!