android在工作线程中处理UI线程报错:Only the original thread that created a view hierarchy can touch its views.
来源:互联网 发布:数据库配置文件 编辑:程序博客网 时间:2024/06/08 17:01
解决办法:异步更新UI
android要求只能在UI线程中更新UI,要想在其他线程中更新UI,就需要异步来处理。
Android异步更新UI的四种方式:
1.使用Handler消息传递机制;
2.使用AsyncTask异步任务;
3.使用runOnUiThread(action)方法;
4.使用Handler的post(Runnabel r)方法;
下面分别使用四种方式来更新一个TextView。
1.使用Handler消息传递机制(非常经典)
import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.widget.TextView; public class MainActivity extends Activity { private TextView tv; //创建handlerHandler handler = new Handler() { public void handleMessage(android.os.Message msg) { if(msg.what==0) //如果消息是刚才发送的标识 { tv.setText("接收到handler发过来的消息进行UI更新"); } }; }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv = (TextView) findViewById(R.id.tv); new MyThread().start(); } class MyThread extends Thread { @Override public void run() { //延迟两秒更新 try { Thread.sleep(2000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } handler.sendEmptyMessage(0); //工作线程的handler发送消息 } } }
2. 使用AsyncTask异步任务
注:更新UI的操作只能在onPostExecute(String result)方法中。
import android.app.Activity; import android.os.AsyncTask; import android.os.Bundle; import android.widget.TextView; public class MainActivity extends Activity { private TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv = (TextView) findViewById(R.id.tv); new MyAsyncTask().execute(); } class MyAsyncTask extends AsyncTask<String, String, String> { @Override protected String doInBackground(String... params) { try { Thread.sleep(2000); //睡2秒 } catch (InterruptedException e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(String result) { // TODO Auto-generated method stub tv.setText("异步更新TextView内容"); } } }
3. 使用runOnUiThread(action)方法(最简单的方法)
import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class MainActivity extends Activity { private TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv = (TextView) findViewById(R.id.tv); new MyThread().start(); } class MyThread extends Thread { @Override public void run() { runOnUiThread(new Runnable() { @Override public void run() { try { //延迟两秒更新 Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } tv.setText("在UI线程更新TextView内容"); } }); } } }
4. 使用Handler的post(Runnabel r)方法
import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.widget.TextView; public class MainActivity extends Activity { private TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv = (TextView) findViewById(R.id.tv); Handler handler = new Handler(); handler.post(new Runnable(){ @Override public void run() { try { //延迟两秒更新 Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } tv.setText("异步更新TextView内容"); } }); } }
0 0
- android在工作线程中处理UI线程报错:Only the original thread that created a view hierarchy can touch its views.
- 在工作线程中处理UI线程报错:Only the original thread that created a view hierarchy can touch its views.
- 在工作线程中处理UI线程报错:Only the original thread that created a view hierarchy can touch its views.
- 在工作线程中处理UI线程报错: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子线程控制UI线程,以防:Only the original thread that created a view hierarchy can touch its views.
- dialog子线程操作UI异常:Only the original thread that created a view hierarchy can touch its views
- dialog子线程操作UI异常: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.
- android studio 线程安全Only the original thread that created a view hierarchy can touch its views
- Work 工作子线程更新UI控件--解决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错误解决
- 报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错误解决
- Only the original thread that created a view hierarchy can touch its views异常处理思路
- Android Only the original thread that created a view hierarchy can touch its views.
- Android单元测试 Only the original thread that created a view hierarchy can touch its views.
- android:Only the original thread that created a view hierarchy can touch its views
- post请求
- 简单使用AsyncTask
- java中的值传递和引用传递问题
- Calendar类
- 进程间通信方式
- android在工作线程中处理UI线程报错:Only the original thread that created a view hierarchy can touch its views.
- Yii框架中的model赋值不成功
- 在Android 5.0中使用JobScheduler
- 使用jQuery操作DOM
- 安卓模拟器Genymotion 不能启动虚拟机的解决方案
- 使用类和对象
- 【java并发】基础(3)--sleep、join、yield
- linux tasklet和workqueue的应用情景
- 使用文本输入提示的方式验证贵美网站的注册页面