当前位置: 首页> 娱乐> 八卦 > Android 为什么子线程不能更新UI

Android 为什么子线程不能更新UI

时间:2025/7/19 14:40:53来源:https://blog.csdn.net/ChenYiRan123456/article/details/141531935 浏览次数:0次

开了线程,更新UI 一般会调用setText(),setImageView 回调到这里面来 ViewRootImpl checkThread(),checkThread()用来检测线程。

    void checkThread() {if (mThread != Thread.currentThread()) {throw new CalledFromWrongThreadException("Only the original thread that created a view hierarchy can touch its views.");}}

其中的mThread在构造函数中赋值mThread = Thread.currentThread();,是主线程

    public ViewRootImpl(Context context, Display display, IWindowSession session,boolean useSfChoreographer) {
//---------mThread = Thread.currentThread();
//-----------
}

而如果在子线程中更新UI,调用checkThread的时候Thread.currentThread()就是子线程,然后和mThread 不相等,就会抛出异常。

关键字:Android 为什么子线程不能更新UI

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: