How to make a "dumb" progress bar?
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;
}
}
i have this code that throws an error. i need a pbar that shows till success is = something other than 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: E开发者_开发知识库RROR/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
that is the logcat im getting. i know this is something that is super simple, but it is escaping me. any advice will be appreciated. thx
updated code:
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;
}
}
the error went away tho, but you never see the dialog.
The problem you are experiencing is because you are executing the thread which barely gets a chance to start befor it finishes. Then after it has died you are dowloading with your helper. You need to move the download code into the doInBackground method of the AsyncTask.
This is more along the lines of what is necessary:
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]);
}
}
For more information please check out the AsyncTask developer docs
In AsyncTask you should setup your UI activities including ProgressDialog in the onPreExecute/onPostExecute like so:
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;
}
}
精彩评论