json数据显示到listview上报错:android.view.ViewRootImpl$CalledFromWrongThreadException解决办法

写项目时候使用okhttp解析json数据显示到listview发现数据解析成功但是报错:android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.


解决办法:

1、把listview调用适配器的地方代码修改成:

	Message message = new Message();
					message.what = 1;
					handler.sendMessage(message);


2、定义一个handler:


	public Handler handler = new Handler() {


		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				Mybaseadapter list_item=new Mybaseadapter();//把适配器放到这里
				lv.setAdapter(list_item);
				break;
			}


		}
	}; 


把listview调用适配器放到handleMessage里

然后重新运行就可以!

你可能感兴趣的:(Android)