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();
}