ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy
2017-01-03 11:09
567 查看
我们都知道在非UI线程不能对UI进行操作,比如下面的操作
就会出现上面的错误,这里介绍下对于上面的错误解决方法:
1.通过Activity.runOnUiThread(Runnable)方法解决:
延时的话就的用handler 了;因为主线程不能阻塞,5秒就ANR。
2.和1的原理是一样的,就是通过Handler,
long)
上面的1.3方法看着代码有些恶心 (当然上面的那种2b操作一般是不会出现的,我主要是投方便的,你可以把那个I'm strong 数据信息的来源想象成从网络请求这种阻塞UI线程的操作而来的),这个时候你可以用2来简化代码,或者使用Asynctask,
4.还有一种就是通过AsyncTask,
这个怎么说呢,我是个偷懒的人,所以直接拖官方的解释吧,传送门https://developer.android.com/guide/components/processes-and-threads.html#Threads
要使用它,必须创建
要更新 UI,应该实现
UI 线程中运行,以便您安全地更新 UI。 稍后,您可以通过从 UI 线程调用
主要代码
ok了,
findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { new Thread(new Runnable() { @Override public void run() { tv.setText( "I'm strong"); } }).start(); } });
就会出现上面的错误,这里介绍下对于上面的错误解决方法:
1.通过Activity.runOnUiThread(Runnable)方法解决:
findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { new Thread(new Runnable() { @Override public void run() { Activity.this.runOnUiThread(new Runnable() { @Override public void run() { tv.setText( "I'm strong"); } }); } }).start(); } });
延时的话就的用handler 了;因为主线程不能阻塞,5秒就ANR。
2.和1的原理是一样的,就是通过Handler,
findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { new Thread(new Runnable() { @Override public void run() { mhandler.sendEmptyMessage(0); } }).start(); } });
Handler mhandler = new Handler(){ @Override public void handleMessage(Message msg) { tv.setText( "I'm strong"); } };3.通过View.post(Runable),这个View就是要操作的View,如果要延时的话用View.postDelayed(Runnable,
long)
findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { new Thread(new Runnable() { @Override public void run() { tv.post(new Runnable() { @Override public void run() { tv.setText( "I'm strong"); } }); } }).start(); } });
上面的1.3方法看着代码有些恶心 (当然上面的那种2b操作一般是不会出现的,我主要是投方便的,你可以把那个I'm strong 数据信息的来源想象成从网络请求这种阻塞UI线程的操作而来的),这个时候你可以用2来简化代码,或者使用Asynctask,
4.还有一种就是通过AsyncTask,
这个怎么说呢,我是个偷懒的人,所以直接拖官方的解释吧,传送门https://developer.android.com/guide/components/processes-and-threads.html#Threads
AsyncTask允许对用户界面执行异步操作。 它会先阻塞工作线程中的操作,然后在 UI 线程中发布结果,而无需您亲自处理线程和/或处理程序。
要使用它,必须创建
AsyncTask的子类并实现
doInBackground()回调方法,该方法将在后台线程池中运行。
要更新 UI,应该实现
onPostExecute()以传递
doInBackground()返回的结果并在
UI 线程中运行,以便您安全地更新 UI。 稍后,您可以通过从 UI 线程调用
execute()来运行任务。
主要代码
public void onClick(View v) { new TvSetTextTask().execute("I'm strong"); } private class TvSetTextTask extends AsyncTask<String, Void, String> { /** The system calls this to perform work in a worker thread and * delivers it the parameters given to AsyncTask.execute() */ protected String doInBackground(String... urls) { return urls[0]; } * the result from doInBackground() */ protected void onPostExecute(String result) { tv.setText(result); } }
ok了,
相关文章推荐
- android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
- android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a vi
- android ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hi
- android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a vi
- android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a v
- android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a vi
- android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a vi
- android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a vi
- android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a vi
- android.view.viewroot$calledfromwrongthreadexception: only the original thread that created a view h
- android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view
- CalledFromWrongThreadException:Only the original thread that created a view hierarchy can touch its
- 16-07-24 android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that
- calledfromwrongthreadexception only the original that created a view hierarchy can touch its views
- CalledFromWrongThreadException: Only the original thread that created a view
- android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original
- Android异常:android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original
- 子线程中处理UI,抛异常 android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original
- CalledFromWrongThreadException: Only the original thread that created a view
- Android异常:android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original