java在Android上加载数据时显示旋转轮对话框
当我的应用程序加载一些数据时,我想显示一个旋转的轮子对话框:
点击一个按钮就会显示“旋转轮子”对话框。我正在使用下面的代码,但它现在确实显示了纺车。有什么问题吗
public void CheckAccount(String username, String password) {
try {
final ProgressDialog progDailog = ProgressDialog.show(this,
"Progress_bar or give anything you want",
"Give message like ....please wait....", true);
new Thread() {
public void run() {
try {
// sleep the thread, whatever time you want.
sleep(1000);
} catch (Exception e) {
}
progDailog.dismiss();
}
}.start();
//getting data code here
//getting data code here
//getting data code here
//getting data code here
//getting data code here
} catch (Exception e) {
Log.e(LOG_TAG, e.getMessage());
PopIt("CheckAccountError", e.getMessage(), "Denied");
}
}
# 1 楼答案
因此,鉴于这个答案得到越来越多的赞成票,我决定自己来提高答案的质量。当我查看原文时,我没有看到“我的代码中有什么问题?”这个问题的真正“答案”。为了保留链接,我保留了下面的原始答案,我认为这是导致答案流行的原因
更新答案
您很可能违反了“单线程模型”。Android UI工具包不是线程安全的,只能从“主”UI线程进行操作。Android有一些有用的方法可以用来确保用户界面操作在用户界面线程上完成。这些方法调用的大部分细节都可以在安卓无痛线程博客帖子中找到(在下面的单词“one”中链接,here供快速参考)
看看你的代码,我看到的具体冲突是,一个
ProgressDialog
正在UI线程中创建,然后在新创建的后台线程中取消最好将后台方法封装在} 在后台完成工作
Runnable
中,并使用^{记住,有很多方法可以做后台工作,所以看看有什么可用的,并使用适合你情况的方法
原始答案
以一个关于如何在a9中做a8的例子为例
此外,这里还有一些类似的问题
Progress dialog problem in Android
Progress Dialog on open activity
Progress Dialog while starting new activity
Android: Progress Dialog spinner not spinning
Updating progress dialog
android: showing a progress dialog