【问题标题】:How to make a "dumb" progress bar?如何制作一个“哑”的进度条?
【发布时间】:2011-12-02 14:32:00
【问题描述】:
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。我知道这是非常简单的事情,但它正在逃避我。任何建议将被认真考虑。谢谢

更新代码:

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;
    }
}

错误消失了,但你永远看不到对话框。

【问题讨论】:

标签: android android-asynctask


【解决方案1】:

您遇到的问题是因为您正在执行的线程在完成之前几乎没有机会启动。然后在它死后你正在下载你的助手。您需要将下载代码移动到 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

【讨论】:

  • 哈,我傻了,我只是假设他已经理解了那部分!
  • 我可能是错的,但我很确定你必须返回 null;如果返回类型是 Void。
  • 你确实是correct我没看到大写的“V”,但是这里没有必要使用Void类
  • 嗯,我在想,因为 AsyncTask 你实际上必须使用 Void 作为返回类型:developer.android.com/reference/android/os/AsyncTask.html ...
  • 可能更好地从下载函数中实际返回真正的返回类型,以便结果在执行后可用...查看我的更新
【解决方案2】:

在 AsyncTask 中,您应该设置 UI 活动,包括 onPreExecute/onPostExecute 中的 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;
       }
    }

【讨论】:

  • 这正是我写的;)
  • 不起作用,progressdialog 从不显示。更新代码见上文。
  • 对,您必须将下载代码放入 doInBackground!
猜你喜欢
  • 2011-04-26
  • 1970-01-01
  • 2016-11-25
  • 1970-01-01
  • 2012-02-07
  • 2013-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多