private void Load()
{
new pbar().execute();
//dialog = ProgressDialog.show(Display.this, "", "Loading. Please wait...", true);
success = faHelper.SourceDownload(Url);
displaystuff();
//dialog.dismiss();
}
private class pbar extends AsyncTask<Void, Void, Void>
{
protected Void doInBackground(Void...unused)
{
dialog = ProgressDialog.show(Display.this, "", "Loading. Please wait...", true);
while (success == 0) {}
dialog.dismiss();
return null;
}
}
我有这个代码会抛出错误。我需要一个显示直到成功的pbar = 0以外的东西。
10-06 16:34:52.802: ERROR/AndroidRuntime(6055): FATAL EXCEPTION: AsyncTask #1
10-06 16:34:52.802: ERROR/AndroidRuntime(6055): java.lang.RuntimeException: An error occured while executing doInBackground()
10-06 16:34:52.802: ERROR/AndroidRuntime(6055): at android.os.AsyncTask$3.done(AsyncTask.java:200)
10-06 16:34:52.802: ERROR/AndroidRuntime(6055): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
10-06 16:34:52.802: ERROR/AndroidRuntime(6055): at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
10-06 16:34:52.802: ERROR/AndroidRuntime(6055): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
10-06 16:34:52.802: ERROR/AndroidRuntime(6055): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
10-06 16:34:52.802: ERROR/AndroidRuntime(6055): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
10-06 16:34:52.802: ERROR/AndroidRuntime(6055): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
10-06 16:34:52.802: ERROR/AndroidRuntime(6055): at java.lang.Thread.run(Thread.java:1027)
10-06 16:34:52.802: ERROR/AndroidRuntime(6055): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
10-06 16:34:52.802: ERROR/AndroidRuntime(6055): at android.os.Handler.<init>(Handler.java:121)
10-06 16:34:52.802: ERROR/AndroidRuntime(6055): at android.app.Dialog.<init>(Dialog.java:145)
10-06 16:34:52.802: ERROR/AndroidRuntime(6055): at android.app.AlertDialog.<init>(AlertDialog.java:63)
10-06 16:34:52.802: ERROR/AndroidRuntime(6055): at android.app.ProgressDialog.<init>(ProgressDialog.java:80)
10-06 16:34:52.802: ERROR/AndroidRuntime(6055): at android.app.ProgressDialog.<init>(ProgressDialog.java:76)
10-06 16:34:52.802: ERROR/AndroidRuntime(6055): at android.app.ProgressDialog.show(ProgressDialog.java:101)
10-06 16:34:52.802: ERROR/AndroidRuntime(6055): at android.app.ProgressDialog.show(ProgressDialog.java:90)
10-06 16:34:52.802: ERROR/AndroidRuntime(6055): at com.Testapp3.Display$pbar.doInBackground(Display.java:132)
10-06 16:34:52.802: ERROR/AndroidRuntime(6055): at com.Testapp3.Display$pbar.doInBackground(Display.java:1)
10-06 16:34:52.802: ERROR/AndroidRuntime(6055): at android.os.AsyncTask$2.call(AsyncTask.java:185)
10-06 16:34:52.802: ERROR/AndroidRuntime(6055): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
10-06 16:34:52.802: ERROR/AndroidRuntime(6055): ... 4 more
这是我正在获取的logcat。我知道这是一件非常简单的事情,但它正在逃避我。任何建议将被认真考虑。 THX
更新代码:
private void Load()
{
new pbar().execute();
success = faHelper.SourceDownload(Url);
displaystuff();
}
private class pbar extends AsyncTask<Void, Void, Void>
{
@Override
protected void onPreExecute()
{
dialog = ProgressDialog.show(Display.this, "", "Loading. Please wait...", true);
}
@Override
protected void onPostExecute(Void result)
{
dialog.dismiss();
return;
}
protected Void doInBackground(Void...unused)
{
while (success == 0) {}
return null;
}
}
错误消失了,但你永远不会看到对话框。
答案 0 :(得分:3)
在AsyncTask中,您应该在onPreExecute / onPostExecute中设置您的UI活动,包括ProgressDialog,如下所示:
private void Load()
{
new pbar().execute();
}
private class pbar extends AsyncTask<Void, Void, Void>
{
@Override
protected void onPreExecute() {
dialog = ProgressDialog.show(Display.this, "", "Loading. Please wait...", true);
}
@Override
protected void onPostExecute(Void result) {
dialog.dismiss();
displaystuff();
return;
}
protected Void doInBackground(Void...unused)
{
success = faHelper.SourceDownload(Url);
return null;
}
}
答案 1 :(得分:3)
您遇到的问题是因为您执行的线程几乎没有机会启动它完成。在它死后你和你的助手一起下载。您需要将下载代码移动到AsyncTask的doInBackground方法中。
这更符合必要:
private void Load()
{
new pbar().execute(url);
}
private class pbar extends AsyncTask<Url, Void, Long>
{
private final String message = "Loading. Please wait...";
private int success=0;
@Override
protected void onPreExecute()
{
dialog = ProgressDialog.show(Display.this, "", message, true);
}
@Override
protected void onPostExecute(Long result)
{
dialog.dismiss();
Display.displaystuff();
}
protected Long doInBackground(Url... urls)
{
return faHelper.SourceDownload(urls[0]);
}
}
有关详细信息,请查看AsyncTask developer docs