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();
}
上一篇:关于vue生命周期遇到的坑???


下一篇:Laravel 查询数据按照时间分组