首页 > 其他分享> > Android studio :后台修改前台界面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
作者:互联网
Only the original thread that created a view hierarchy can touch its views
这句话的意思是:只有创建这个view的线程才能操作这个view
解决方法就是handle
1、
Handler mHandler;
2、
mHandler = new Handler();//创建Handler
3、
//放在主线程或新开线程都可以
Runnable mRunnable0 = new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
}
};
4、线程中
mHandler.post(mRunnable0);
5、完整代码
public class MainActivity extends MyApplication{
Handler mHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mHandler = new Handler();//创建Handler
new Thread(new Runnable() {
@Override
public void run() {
//......
//放在主线程或新开线程都可以
Runnable mRunnable0 = new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
}
};
//在线程中
mHandler.post(mRunnable0);
}
}).start();
}
标签:Runnable,thread,created,hierarchy,Override,mHandler,Handler,new,public 来源: https://blog.csdn.net/cui_xiaoyu/article/details/105582761