Android AlertDialog Box not showing

I searched through Stackoverflow, looked at examples in Android Developer and some books, and I just don't get it.

I am trying to show an AlertDialog window that interrupts the program flow. Most of the examples I saw do not have code after the dialog box, and I need my program to stop executing until the AlertDialog button is clicked. It seems that AlertDialog is created from another thread, and I'm not sure how to extract this thread.

Program logic: if the parsing is bad, the program will be forced to close. I want to tell the user to restart the program, and everything will work. (I drop and recreate the tables, and they populate when the program starts)

Here is the code:

if(database populated) { ....code..... if(parseok.contentEquals("Error")) { doForceClose(); } displayDate = "Last: " + parseok; //I don't want the program to continue to here. //Rest of code in the method. If I continue the program will Force Close } else do something else 

Here's the AlertDialog method:

 private void doForceClose() { String themessage = "Because some of the parameters have changed in the yada yada"; AlertDialog.Builder ad = new AlertDialog.Builder (this); ad.setTitle("Internal Error"); ad.setMessage(themessage); ad.setPositiveButton("Sorry", new OnClickListener() { public void onClick(DialogInterface dialog, int which) { finish(); return; } }); ad.create(); ad.show(); } 

except for the ad is not displayed, and the program continues to close its power.

Obviously, I am not getting anything. Any ideas?

edit: I'm in a class that extends Activity

+4
source share
3 answers

I am trying to show an AlertDialog window that interrupts the program flow.

It does not exist in Android and various other user interfaces.

I need my program to stop execution until the AlertDialog button is pressed.

No no. Event driven programming has been used for several decades.

Program logic: if the parsing is bad, the program will be forced to close.

What is your code - rewrite it to behave better.

I do not want the program to continue here.

Then use else or return or something else.

except for the ad is not displayed, and the program continues to close its power.

Your dialogue will not be displayed until the main thread of the application receives control again to process your request. show() is asynchronous. You used to crash, most likely.

In short, your strategy for solving your parsing problem is fundamentally wrong.

+6
source

Commonsware's answer is correct. Let me try to say the same thing in different words. A warning dialog box DOES NOT interrupt the control flow. It is just β€œleft” when the program is waiting for input.

thus, the sequence showAlert ("this message); gallery show (); return;

it is displayed only instantly. The way out of this is to call the showGallery () function inside a positive response from AlertDialog.

So, in a different way. If you want to interrupt the flow of your application using AlertDialog (which is reasonably indicated, this is not what you need), then enter the code that you want to execute after the dialog into the onClick callback from AlertDialog.

+2
source

Well, I had a similar situation. I was expecting a dialog to open, but that is not the case. Instead, an exception occurred. The place where the exception occurred was located a couple of lines behind the place where I was expecting dialogue. I fixed this exception and then my dialog popped up.

This looks strange, and I think that the time should appear in the dialog box while the program just continues to work, and then the program encounters an exception, chronological (but not programmatic) before the dialog. This is why I got the dialog after I fixed the exception place.

0
source

Source: https://habr.com/ru/post/1340464/


All Articles