Kri*_*ris 38 java android dialog loading progress
我想在我的应用加载一些数据时显示一个旋转轮对话框:

按钮单击时应显示旋转轮对话框.我正在使用下面的代码,但它现在显示了旋转轮.可能是什么问题呢?
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");
}
}
Run Code Online (Sandbox Code Playgroud)
nic*_*ild 49
因此,看到这个答案越来越受欢迎,我决定自己应该自己提高答案的质量.当我看到原文时,我看不出"我的代码中的问题是什么?"这个问题没有真正的"答案".我将原始答案保留在下面,以保留链接,这是我假设已经导致答案流行.
更新的答案
您可能违反了"单线程模型".Android UI Toolkit不是线程安全的,只能从"主"UI线程进行操作.Android有一些有用的方法可用于确保您的UI操作在UI线程上完成.这些方法调用的大部分细节都可以在Android无痛线程博客文章中找到(链接在下面的"一个"字样中,此处可供快速参考).
看看你的代码,我看到的具体违规是ProgressDialog在可能的UI线程中创建了一个,然后在新创建的后台线程中被解散.
最好将背景方法封装在一起Runnable并View#post(Runnable)用于在后台完成工作.
请记住,有很多方法可以做背景,所以看一下可用的方法,并使用适合您情况的方法.
原始答案
快来看看在一个对的许多 教程如何做异步 工作在Android的.
此外,以下是一些类似的其他StackOverflow问题
Abh*_*bhi 44
ProgressDialog dialog = new ProgressDialog(this); // this = YourActivity
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
dialog.setTitle("Loading");
dialog.setMessage("Loading. Please wait...");
dialog.setIndeterminate(true);
dialog.setCanceledOnTouchOutside(false);
Run Code Online (Sandbox Code Playgroud)
dialog.show();
Run Code Online (Sandbox Code Playgroud)
dialog.dismiss();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
76795 次 |
| 最近记录: |