android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a vi

android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views

这个错误相信大家一定不陌生,特别是刚学习android时,会引起的错误,意思是指在子线程中对UI做了操作,现在写个简单的demo:

public class MainActivity extends Activity {

private TextView textview;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

textview = (TextView) findViewById(R.id.textview);

new Thread(){

public void run() {

try {

Thread.sleep(1000);

textview.setText("hello");

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}.start();

}

}

发现我们是在子线程中对TextView控件中添加内容,导致的错误产生!在此记录下

时间: 2024-10-25 19:03:08

android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a vi的相关文章

android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

1 runOnUiThread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub Log.i(tag, "will-success-update"); adapter.addItem((Will)reason); } }); Done

浅析Android中的消息机制-解决:Only the original thread that created a view hierarchy can touch its views.

在分析Android消息机制之前,我们先来看一段代码: [html] view plaincopyprint? public class MainActivity extends Activity implements View.OnClickListener { private TextView stateText; private Button btn; @Override public void onCreate(Bundle savedInstanceState) { super.onC

Android: Only the original thread that created a view hierarchy can touch its views 异常

最近自己再写一个小项目练手,创建一个线程从网络获取数据然后显示在 recyclerView 上.写好后发现页面能够显示,但是有时候会把请求的数据显示过来,有时候不会.点开 android monitor 一看,有一个提示 : Only the original thread that created a view hierarchy can touch its views. 异常的意思是说只有创建这个view的线程才能操作这个 view,普通会认为是将view创建在非UI线程中才会出现这个错误.

android异常:android.view.ViewRootImpl$CalledFromWrongThreadException

android异常:android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. 出现这个异常的原因是你在子线程中修改了主线程中的组件的状态或数据. android规定:只有在原始线程中(主线程,UI线程)才能修改显示组件.

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

写代码的时候碰到android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.这个异常.异常的意思是说只有创建这个view的线程才能操作这个view,普通会认为是将view创建在非UI线程中才会出现这个错误.可是在我代码中将view创建在UI线程中也会出现这个错误 下面是我出错的代码: Asyn

android.view.ViewRootImpl$CalledFromWrongThreadException错误处理

一般情况下,我们在编写android代码的时候,我们会将一些耗时的操作,比如网络访问.磁盘访问放到一个子线程中来执行.而这类操作往往伴随着UI的更新操作.比如说,访问网络加载一张图片 new Thread() { @Override public void run() { try { URL url = new URL(path); HttpURLConnection connection = (HttpURLConnection) url .openConnection(); // 设置请求方

android.view.ViewRoot$CalledFromWrongThreadException的解决办法

android 是不允许子线程直接更新UI的,如果一定要在子线程直接更新UI就会出现android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.大概意思就是说 只有原来创建找个视图hierarchy的线程才可以修改修改它. 我的解决办法是使用Activity.runOnUiThread(Runnabl

错误:Only the original thread that created a view hierarchy can touch its views——Handler的深入解析

这个错误很常见,基本上写线程操作都遇到过这个错误.根本原因是view控件的线程安全问题,通俗点讲就是所有的更新UI操作都需要在主线程(也就是UI线程中完成),而不能在新开的子线程中操作. 基本思路:既然子线程需要更新UI,但子线程自身又不能完成任务,所以只能通过建立一个通信机制,当子线程需要更新UI时,发消息通知主线程并将更新UI的任务post给主线程,让主线程来完成分内的UI更新操作.这个机制是什么呢?就是Handler.Handler 从属于谁?当然是主线程.每个线程都有自己的handler

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的时候出现这个错误,是用错了方法. * * 2016-6-15 深圳 南山平山村 曽剑锋 ****************************