gpt4 book ai didi

Android ProgressDialog运行时异常错误

转载 作者:行者123 更新时间:2023-11-29 18:21:04 24 4
gpt4 key购买 nike

我在尝试将 ProgressDialog 放入我的应用程序时遇到问题。在我的 GameEngine 类(不扩展任何内容)中,我有如下所示的代码。第一行产生一个运行时异常,虽然我遇到了这个似乎是相同错误的线程:Android TimerTask throws RuntimeException if Show ProgressDialog is added in run() ,我真的不明白如何实现解决方案。任何帮助将不胜感激,谢谢。

    //Create ProgressDialog
ProgressDialog dialog = ProgressDialog.show(context, "",
"Loading...", true);

//Set Clusters before level starts
for (int i = 0; i < 80; i++)
{
updateBacteria();
updateAttraction();
checkCollisions();
moveObjectsAwayFromWalls();
}

dialog.dismiss();

最佳答案

您只能在 UI 线程(扩展 Activity 的主类)中显示对话框。为此,您可以编写一个 Handler 并使用它从非 UI 线程向 UI 线程发送消息。 Android 有一个这样的例子 in their ProgressDialog example.查看他们在“Example ProgressDialog with a second thread”下的代码 fragment 。

您也可以按照您提供的链接的答案中所写的相同方法,尽管 Handler 是一种更可靠的方法。

关于Android ProgressDialog运行时异常错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5366104/

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