美文网首页
Only the original thread that cr

Only the original thread that cr

作者: 晴天ccc | 来源:发表于2022-02-27 09:29 被阅读0次

前言

在使用Retrofit+OkHttp网络处理的时候,遇到如下错误
Only the original thread that created a view hierarchy can touch its views
翻译过来:只有创建这个View的线程才能操作这个View。就是没有在指定线程操作Ui。

解决方案一

在对应的回调接口根据网络请求返回的数据来传递不同的消息

handler.sendMessage(msg);

再根据消息的内容执行进一步操作

    private Handler handler = new Handler()
    {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what)
            {
                case 1:
                    ...
                    break;
            }
        }
    };

解决方案二

        MainActivity.this.runOnUiThread(new Runnable() {
            public void run() {
                // 目前已经处于Ui线程了
            }
        });

相关文章

网友评论

      本文标题:Only the original thread that cr

      本文链接:https://www.haomeiwen.com/subject/fixlrrtx.html