正常情況下,Android需要在UI線程更新UI,然鵝,在特殊情況下,子線程也能更新UI不在討論之列,可參考Android中子線程真的不能更新UI嗎?這篇文章主要講一下個人理解的正常情況下為什麼不能在非UI線程更新UI。
先拿一句話來鎮樓android.view.ViewRootImpl$CalledFromWrongThreadException:Onlytheoriginalthreadthatcreatedaviewhierarchycantouchitsviews.然後曬出Android官方的一句話來說:「The Android UI toolkit is not thread-safe and the view must always be manipulated on the UI thread.」 因為Android UI操作並不是線程安全的,並且這些操作必須在UI線程執行。我們就主要分析一下這句話背後包含的含義。