gpt4 book ai didi

Android AlertDialog 不显示

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:25:24 25 4
gpt4 key购买 nike

我搜索过 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

这是 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();
}

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

显然我没有得到任何东西。有什么想法吗?

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

最佳答案

I'm trying to show an AlertDialog Box that interrupts the program flow.

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

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

不,你不知道。事件驱动编程已经使用了几十年。

Program logic: If the parsing is bad the program will force close.

那是你的代码——重写它以表现得更好。

I don't want the program to continue to here.

然后使用else,或return,或其他东西。

except ad doesn't show and the program continues to its force close.

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

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

关于Android AlertDialog 不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5052462/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com