2011-10-06 76 views
-1
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

非常相似http://stackoverflow.com/q/7340412/324625 –

+1

你是否从UI线程调用'Load()',比如从一个activity? –

回答

3

您所遇到的问题是,因为你正在执行它几乎没有得到机会开始befor它的线程饰面。然后,它死了,你正在下载与你的助手。您需要将下载代码移入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

+0

哈,愚蠢的我,我只是假定他已经理解了那部分! –

+0

我可能是错的,但我很确定你必须返回null;如果返回类型是Void。 –

+0

事实上,你是[正确](http://stackoverflow.com/q/676663/808940)我没有发现大写字母'V',但没有必要在这里使用Void class – Merlin

3

在的AsyncTask,您应该设置你的UI活动,包括ProgressDialog在onPreExecute/onPostExecute像这样:

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

这正是我写的;) –

+0

不工作,progressdialog从不显示。请参阅上面的更新代码。 – texasman1979

+0

对,你必须把你的下载代码放到doInBackground中! –