Android AlertDialog框未显示

eri*_*ric 4 android android-alertdialog

我搜索了Stackoverflow,查看了Android Developer中的示例,以及一些书籍,我只是没有得到它.

我正在尝试显示一个中断程序流的AlertDialog Box.我见过的大多数例子都没有对话框后的代码,我需要程序停止执行,直到按下AlertDialog按钮.好像AlertDialog是从另一个线程创建的,我不知道如何检索该线程.

程序逻辑:如果解析不好,程序将强制关闭.我想让用户知道重新启动程序,一切都会正常工作.(我正在删除并重新创建表,并在程序启动时重新填充它们)

这是一些代码:

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
Run Code Online (Sandbox Code Playgroud)

这是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();        
}
Run Code Online (Sandbox Code Playgroud)

除了广告没有显示,程序继续强制关闭.

显然我没有得到什么.有任何想法吗?

编辑:我在一个扩展Activity的类中

Com*_*are 6

我正在尝试显示一个中断程序流的AlertDialog Box.

这在Android和其他各种UI系统中都不存在.

我需要我的程序停止执行,直到按下AlertDialog按钮.

不,你没有.事件驱动编程已经使用了几十年.

程序逻辑:如果解析不好,程序将强制关闭.

这是你的代码 - 重写它以表现得更好.

我不希望程序继续在这里.

然后使用else,或return,或等.

除了广告没有显示,程序继续强制关闭.

在主应用程序线程再次获得控制权来处理您的请求之前,您的对话框不会出现 - show()是异步的.你很可能在那之前崩溃.

简而言之,您处理解析问题的策略存在根本缺陷.