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;
}
}
错误走了寿,但你永远看不到的对话框。
非常相似http://stackoverflow.com/q/7340412/324625 –
你是否从UI线程调用'Load()',比如从一个activity? –