當我爲Android編寫某種HTML下載器時,我決定使用ProgressBar,但它拋出了java.lang.IllegalStateException: The current thread must have a looper!
。我知道這個異常在函數需要在主線程中調用時拋出,但我在runOnUiThread()
中調用它請查看下面的代碼。ProgressBar looper異常runOnUiThread
//Inside a worker thread's run()
progressbar.setInterminate(true); //No Exception here
//Connect and get InputStream.
progressbar.setMax(conn.getContentLength()); //No exception here, too
progressbar.setProgress(0); //No exception here.
runOnUiThread(new Runnable() {
@Override
public void run() {
progressbar.setInterminate(false); //Exception here!
}
});
我已經試過移動setProgress()
低於setInterminate(false)
但什麼也沒做。
在此先感謝。
我假設你的工作線程只運行一次下載的東西?是對的嗎? –
@Inferno你是對的。也許我應該使用一個共享的值來存檔它? – minary
什麼是「進步」?它來自哪裏?您似乎有兩個不同的進度條對象:'progressbar'和'progress'。請提供更多細節,並可能添加堆棧跟蹤。 –