`

Only the original thread that created a view hierarchy can touch its views.

阅读更多
//Only the original thread that created a view hierarchy can touch its views.
//不能在非UI线程里更新UI



public class MainActivity extends AppCompatActivity {

    private Handler handler;

    @BindView(R.id.get_btn)
    Button get_btn;

    @BindView(R.id.post_btn)
    Button post_btn;

    @BindView(R.id.result_view)
    TextView result_view;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        //主线程的handler
        handler = new Handler();

    }
    @OnClick(R.id.get_btn)
    public void getMsg(){
        doGet();
    }
    @OnClick(R.id.post_btn)
    public void postMsg(){



    }

    public void doGet(){
        //创建okhttpClient对象
        OkHttpClient okhttpClient = new OkHttpClient();
        //构建Request
        Request.Builder builder = new Request.Builder();
        Request request = builder.get().url("https://www.baidu.com").build();
        //将Request封装为Call
        Call call = okhttpClient.newCall(request);
        //执行Call
        //异步执行   提供回调接口
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Toast.makeText(MainActivity.this, "网络不给力", Toast.LENGTH_SHORT).show();
            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                final String string = response.body().string();
                // Only the original thread that created a view hierarchy can touch its views.
                //result_view.setText(string);
                new Thread(){
                    @Override
                    public void run() {
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                result_view.setText(string);
                            }
                        });
                    }
                }.start();

            }
        });
    }
}

 

分享到:
评论

相关推荐

    Android studio :后台修改前台界面Only the original thread that created a view hierarchy can touch its views

    Only the original thread that created a view hierarchy can touch its views 这句话的意思是:只有创建这个view的线程才能操作这个view 解决方法就是handle 1、 Handler mHandler; 2、 mHandler = new Handler();...

    开新线程引发的常见3个异常

    2.android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. 3.java.lang.RuntimeException: Can't create handler inside thread ...

    Android 子线程更新UI

    android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. 这个异常为何不是 Only the main thread that created a view hierarchy ...

    详解Android UI更新的几种方法

    如果是在WT进行UI的更新,则会抛出异常,android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.(只有创建这个View的原

    Xamarin.Android 非UI线程更新UI

    如果在非UI线程中直接修改UI元素,系统会抛出`Android.Content.ReceiverNotRegisteredException`或`Java.Lang.RuntimeException: Only the original thread that created a view hierarchy can touch its views.`...

    为什么能在子线程通过setText进行更新UI

    为什么可以在子线程通过setText进行更新UI ... Only the original thread that created a view hierarchy can touch its views.); } } 一般情况下在子线程更新UI是会报错的,因为在ViewRootImpl

    【Android】CalledFromWrongThreadException 深入源码分析

    在使用 dialog 的时候,因为线程问题,在调用 dismiss() 方法的时候,出现如下常见的 crash–Only the original thread that created a view hierarchy can touch its views.,堆栈信息如下: threadName:main,...

    Android延时操作的三种方法

    前两种方法在更新 UI 时,如果不使用消息处理机制的话,会报错,例如 "Only the original thread that created a view hierarchy can touch its views."(只能在主线程中更新 UI)。因此,在使用延时操作时,推荐...

    Android消息机制基本原理和使用

    例如在子线程进行耗时较长的下载,等下载完成之后,再去更新UI,提示用户下载完成,直接在子线程里更新UI,会得到报错提示:Only the original thread that created a view hierarchy can touch its views。...

Global site tag (gtag.js) - Google Analytics