简体   繁体   English

Android AlertDialog框未显示

[英]Android AlertDialog Box Not Showing

I've searched through Stackoverflow, looked at the examples in Android Developer, and some books and I'm just not getting it. 我搜索了Stackoverflow,查看了Android Developer中的示例,以及一些书籍,我只是没有得到它。

I'm trying to show an AlertDialog Box that interrupts the program flow. 我正在尝试显示一个中断程序流的AlertDialog Box。 Most of the examples I've seen don't have code after the dialog box and I need my program to stop executing until the AlertDialog button is pressed. 我见过的大多数例子都没有对话框后的代码,我需要程序停止执行,直到按下AlertDialog按钮。 Seems as if the AlertDialog is created from another thread and I'm not sure how to retrieve that thread. 好像AlertDialog是从另一个线程创建的,我不知道如何检索该线程。

Program logic: If the parsing is bad the program will force close. 程序逻辑:如果解析不好,程序将强制关闭。 I want to let the user know to restart the program and everything will work. 我想让用户知道重新启动程序,一切都会正常工作。 (I'm dropping and recreating tables and they are repopulated when the program starts back up) (我正在删除并重新创建表,并在程序启动时重新填充它们)

Here's some 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: 这是AlertDialog方法:

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 ad doesn't show and the program continues to its force close. 除了广告没有显示,程序继续强制关闭。

Obviously I'm not getting something. 显然我没有得到什么。 Any ideas? 有任何想法吗?

edit: I am in a Class that extends Activity 编辑:我在一个扩展Activity的类中

I'm trying to show an AlertDialog Box that interrupts the program flow. 我正在尝试显示一个中断程序流的AlertDialog Box。

That does not exist in Android and various other UI systems. 这在Android和其他各种UI系统中都不存在。

I need my program to stop executing until the AlertDialog button is pressed. 我需要我的程序停止执行,直到按下AlertDialog按钮。

No, you don't. 不,你没有。 Event-driven programming has been in use for a couple of decades. 事件驱动编程已经使用了几十年。

Program logic: If the parsing is bad the program will force close. 程序逻辑:如果解析不好,程序将强制关闭。

That's your code -- rewrite it to behave better. 这是你的代码 - 重写它以表现得更好。

I don't want the program to continue to here. 我不希望程序继续在这里。

Then use an else , or a return , or something. 然后使用else ,或return ,或其他东西。

except ad doesn't show and the program continues to its force close. 除了广告没有显示,程序继续强制关闭。

Your dialog will not appear until the main application thread gets control again to process your request -- show() is asynchronous. 在主应用程序线程再次获得控制权来处理您的请求之前,您的对话框不会出现 - show()是异步的。 You are crashing before then, most likely. 你很可能在那之前崩溃。

In short, your strategy for dealing with your parsing problem is fundamentally flawed. 简而言之,您处理解析问题的策略存在根本缺陷。

The Commonsware response s correct. Commonsware的回应是正确的。 Let me try and say the same thing in different words. 让我试着用不同的话说同样的话。 An alert dialog does NOT interrupt the flow of control. 警报对话框不会中断控制流。 It is just "left showing" when the program is waiting for input. 当程序等待输入时,它只是“左显示”。

thus the sequence showAlert("this is a message); showGallery(); return; 因此序列showAlert(“这是一条消息); showGallery(); return;

this shows only momentarily. 这只是暂时的。 A way out of this is to put the showGallery() function call inside the Positive response from the AlertDialog. 解决这个问题的方法是将showGallery()函数调用放在来自AlertDialog的Positive响应中。

So to put it another way. 换句话说。 If you want to interrupt the flow of your app with an AlertDialog (which is wisely pointed out is the wrong thing to want) then put the code you want executed after the dialog into the onClick callback of the AlertDialog. 如果您想使用AlertDialog中断应用程序的流程(明智地指出这是错误的想法),那么将对话后要执行的代码放入AlertDialog的onClick回调中。

OK, I had a similar situation. 好的,我有类似的情况。 I was expecting a dialog to pop-up but it didn't. 我期待弹出一个对话框,但事实并非如此。 Instead, an exception occurred. 相反,发生了异常。 The place where the exception occurred was positioned a couple of rows behind the place where I was expecting the dialog. 发生异常的地方位于我期待对话的地方后面几行。 I fixed this exception and then my dialog appeared. 我修复了这个异常然后我的对话框出现了。

It looks strange, and I think the dialog needs time to appear while the program just continues to run and then the program encounter the exception, chronological (but not programatically) before the dialog. 它看起来很奇怪,我认为对话框需要时间才能在程序继续运行时出现,然后程序在对话之前按时间顺序(但不是以编程方式)遇到异常。 That's why I got dialog after I fixed the exception place. 这就是我修复异常位置后得到对话的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM