为什么有时候在子线程更新UI没报错?

| 2019-05-17

看到这个标题,好多人第一时间想到的是什么? 感兴趣的不妨跟着下面的代码看看会发生什么? 首先我在 onCreate 方法里调用 setText() 方法

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mContext = this
        Log.e(TAG, "onCreate")
        Thread {
            val simpleDateFormat = SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss") // HH:mm:ss
            //获取当前时间
            val date = Date(System.currentTimeMillis())
            // 更新TextView文本
            tv_title.text="Date获取当前日期时间" + simpleDateFormat.format(date))
        }.start()

    }

这个时候允许呢,会发现,哎?为什么正常呢,不应该报错吗? 但是呢, 当我改成了这样以后再次运行

        Thread {
            try {
                Thread.sleep(2000)
                val simpleDateFormat = SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss") // HH:mm:ss
                //获取当前时间
                val date = Date(System.currentTimeMillis())
                // 更新TextView文本内容
                tv_title.text = "Date获取当前日期时间" + simpleDateFormat.format(date)
            } catch (e: InterruptedException) {
                e.printStackTrace()
            }
        }.start()

结果

   android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
       at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6557)
       at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:943)
       at android.view.ViewGroup.invalidateChild(ViewGroup.java:5081)
       at android.view.View.invalidateInternal(View.java:12719)
       at android.view.View.invalidate(View.java:12683)
       at android.view.View.invalidate(View.java:12667)
       at android.widget.TextView.checkForRelayout(TextView.java:7167)
       at android.widget.TextView.setText(TextView.java:4347)
       at android.widget.TextView.setText(TextView.java:4204)
       at android.widget.TextView.setText(TextView.java:4179)

这应该就是大家熟悉的报错了吧,不允许在非UI线程中更新UI线程 既然报这个错了,那就跟进去,看看 ViewRootImpl.java 为什么报这个错,之前分享过看源码的方式。 点我看源码 既然报错已经告诉我们在哪一行了,那我们就点进去看看,可以很容易的找到 在这里要说明一下,在Android2.2以后是用ViewRootImpl来代替ViewRoot的,用来连接WindowManager和DecorView,而且View的绘制也是通过ViewRootImpl来完成的。

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

当Activity对象被创建完毕后,将DecorView添加到Window中,同时会创建ViewRootImpl对象,在源码中可以看到 mThread 是在ViewRootImpl 的构造方法里这样初始化的。然后再把他设为主线程。

那现在捋一下,从上面的错误栈里,可以看到调用的流程是:

at android.widget.TextView.setText(TextView.java:4347) at android.widget.TextView.checkForRelayout(TextView.java:7167) at android.view.View.invalidate(View.java:12667) at android.view.View.invalidateInternal(View.java:12719) atandroid.view.ViewGroup.invalidateChild(ViewGroup.java:5081) atandroid.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:943) at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6557)

所以现在聪明的同事是不是知道了,要去看看 ViewRootImpl 这个是在哪里被初始化的?

3126    final void handleResumeActivity(IBinder token,
3127            boolean clearHide, boolean isForward, boolean reallyResume) {
                                    ...
3158            if (r.window == null && !a.mFinished && willBeVisible) {
3159                r.window = r.activity.getWindow();
3160                View decor = r.window.getDecorView();
3161                decor.setVisibility(View.INVISIBLE);
3162                ViewManager wm = a.getWindowManager();
3163                WindowManager.LayoutParams l = r.window.getAttributes();
3164                a.mDecor = decor;
3165                l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
3166                l.softInputMode |= forwardBit;
3167                if (a.mVisibleFromClient) {
3168                    a.mWindowAdded = true;
3169                    wm.addView(decor, l);
3170                }
3171
3172           ...
3247    }

最后的 wm.addView(decor, l) 就是我们要找的答案,这个时候看一下windowManager.addView(decorView)

  public void addView(View view, ViewGroup.LayoutParams params,
232            Display display, Window parentWindow) {
233        if (view == null) {
234            throw new IllegalArgumentException("view must not be null");
235        }
236        if (display == null) {
237            throw new IllegalArgumentException("display must not be null");
238        }
239        if (!(params instanceof WindowManager.LayoutParams)) {
240            throw new IllegalArgumentException("Params must be WindowManager.LayoutParams");
241        }
242
257        ViewRootImpl root;
258        View panelParentView = null;

                 ...
303            mViews.add(view);
304            mRoots.add(root);
305            mParams.add(wparams);
306        }
307
308        // do this last because it fires off messages to start doing things
309        try {
310            root.setView(view, wparams, panelParentView);
311        } catch (RuntimeException e) {
312            // BadTokenException or InvalidDisplayException, clean up.
313            synchronized (mLock) {
314                final int index = findViewLocked(view, false);
315                if (index >= 0) {
316                    removeViewLocked(index, true);
317                }
318            }
319            throw e;
320        }
321    }

看到这里,是不是有种豁然开朗的感觉,因为已经找到了答案,答案就是跟 ViewRootImpl 的初始化有关,因为我之前的代码是在 onCreate() 的时候此时去设置textview,此时呢 View 还没被绘制出来,ViewRootImpl 还未创建,它的创建是在 handleResumeActivity() 的调用到 windowManager.addView(decorView) 时候。

编辑:航网科技 来源:腾讯云 本文版权归原作者所有 转载请注明出处

在线客服

微信扫一扫咨询客服


全国免费服务热线
0755-36300002

返回顶部