2010-09-01 24 views
76

我不明白爲什麼我得到這個錯誤。我使用AsyncTask在後臺運行一些進程。無法在線程內創建處理程序內部沒有在AsyncTask中調用Looper.prepare()ProgressDialog

我:

protected void onPreExecute() 
{ 
    connectionProgressDialog = new ProgressDialog(SetPreference.this); 
    connectionProgressDialog.setCancelable(true); 
    connectionProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); 
    connectionProgressDialog.setMessage("Connecting to site..."); 
    connectionProgressDialog.show(); 

    downloadSpinnerProgressDialog = new ProgressDialog(SetPreference.this); 
    downloadSpinnerProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); 
    downloadSpinnerProgressDialog.setMessage("Downloading wallpaper..."); 
} 

當我進入doInBackground()取決於條件一:

[...]  
connectionProgressDialog.dismiss(); 
downloadSpinnerProgressDialog.show(); 
[...] 

每當我試着downloadSpinnerProgressDialog.show()我收到錯誤。

任何想法傢伙?

回答

103

方法show()必須從User-Interface (UI) thread調用,而doInBackground()運行在不同的線程,這是設計AsyncTask的主要原因。

您必須撥打show()或撥打onProgressUpdate()onPostExecute()

例如:

class ExampleTask extends AsyncTask<String, String, String> { 

    // Your onPreExecute method. 

    @Override 
    protected String doInBackground(String... params) { 
     // Your code. 
     if (condition_is_true) { 
      this.publishProgress("Show the dialog"); 
     } 
     return "Result"; 
    } 

    @Override 
    protected void onProgressUpdate(String... values) { 
     super.onProgressUpdate(values); 
     connectionProgressDialog.dismiss(); 
     downloadSpinnerProgressDialog.show(); 
    } 
} 
+3

這並沒有爲我做的伎倆,只是試圖「AlertDialog .Builder builder = new AlertDialog.Builder(context);「並添加了構建器的配置。試圖在onProgressUpdate中顯示這個結果會導致完全相同的錯誤。有任何想法嗎 ? – ins0m 2011-08-30 21:28:18

+0

感謝Konstantin,我沒有意識到你可以用任何使用publishProgress()的變量類型覆蓋onProgressUpdate。真的幫助我,只需更新吐司消息。 – wired00 2012-12-12 03:07:46

75

我也有類似的問題,但是從閱讀這個問題我想我可以在UI線程上運行:

YourActivity.this.runOnUiThread(new Runnable() { 
    public void run() { 
     alertDialog.show(); 
    } 
}); 

似乎做的伎倆我。

+0

thanx工作... – Debasish 2017-12-23 13:26:04

0
final Handler handler = new Handler() { 
     @Override 
     public void handleMessage(final Message msgs) { 
     //write your code hear which give error 
     } 
     } 

new Thread(new Runnable() { 
    @Override 
    public void run() { 
    handler.sendEmptyMessage(1); 
     //this will call handleMessage function and hendal all error 
    } 
    }).start(); 
1

我有一個很難使這項工作過,對我來說,解決方案是使用兩個hyui和康斯坦丁答案,

class ExampleTask extends AsyncTask<String, String, String> { 

// Your onPreExecute method. 

@Override 
protected String doInBackground(String... params) { 
    // Your code. 
    if (condition_is_true) { 
     this.publishProgress("Show the dialog"); 
    } 
    return "Result"; 
} 

@Override 
protected void onProgressUpdate(String... values) { 

    super.onProgressUpdate(values); 
    YourActivity.this.runOnUiThread(new Runnable() { 
     public void run() { 
      alertDialog.show(); 
     } 
    }); 
} 

} 
+0

我認爲在'onProgressUpdate()'中完成的東西會自動在UI線程上運行 – 2013-11-25 19:21:37

相關問題