问题
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