我知道,當我嘗試顯示在後臺線程以下異常ProgressDialog拋出Can't create handler inside thread that has not called Looper.prepare()
,爲什麼拋出後臺線程中的ProgressDialog時不會拋出異常?
,因爲我們正試圖從後臺線程修改UI。但是當我們在後臺線程中關閉對話框時不會引發異常。爲什麼在我們關閉後臺線程中的對話框時拋出異常,因爲我們也從後臺線程修改了UI。
感謝
我知道,當我嘗試顯示在後臺線程以下異常ProgressDialog拋出Can't create handler inside thread that has not called Looper.prepare()
,爲什麼拋出後臺線程中的ProgressDialog時不會拋出異常?
,因爲我們正試圖從後臺線程修改UI。但是當我們在後臺線程中關閉對話框時不會引發異常。爲什麼在我們關閉後臺線程中的對話框時拋出異常,因爲我們也從後臺線程修改了UI。
感謝
下面是對話的解僱()方法的代碼,說明爲什麼沒有異常被拋出和解僱作品:
/**
* Dismiss this dialog, removing it from the screen. This method can be
* invoked safely from any thread. Note that you should not override this
* method to do cleanup when the dialog is dismissed, instead implement
* that in {@link #onStop}.
*/
public void dismiss() {
if (Thread.currentThread() != mUiThread) {
mHandler.post(mDismissAction);
} else {
mDismissAction.run();
}
}
下面是從對話框中.dismiss()方法的實現因爲它是在2.2中:
public void dismiss() {
if (Thread.currentThread() != mUiThread) {
mHandler.post(mDismissAction);
} else {
mDismissAction.run();
}
}
正如你所看到的,它會檢查你是否試圖從UI線程調用它。如果你這樣做,它會自行解散。但是如果你從另一個線程中解僱它,它就會通過一個在UI線程上創建的Handler來處理這個動作,因此它將在UI線程上處理解僱動作。
呃,我遲了1分鐘:) –