.AppException: android.view.ViewRootImpl$CalledFromWrongThreadException: Only t

错误内容:.AppException: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

这是在主线程中使用了销毁时间的操作,容易造成ANR;

解决方案:

private void setData() {
 
  if (null != lineInfos) {
   LineImageAdapter adapter = new LineImageAdapter(
     ((AppContext) getActivity().getApplicationContext()),
     lineInfos, mGridView);
   mGridView.setAdapter(adapter);
   mGridView.setOnItemClickListener(new GridItemClickListener());
  }
 }
改为:private void setData() {
  Message message = new Message();
  message.what = 5;
  handler.sendMessage(message);
 }

 private Handler handler = new Handler() {
  @SuppressWarnings("unchecked")
  @Override
  public void handleMessage(Message msg) {
switch (msg.what) {
case 5:
    if (null != lineInfos) {
     LineImageAdapter adapter = new LineImageAdapter(
       ((AppContext) getActivity().getApplicationContext()),
       lineInfos, mGridView);
     mGridView.setAdapter(adapter);
     mGridView.setOnItemClickListener(new GridItemClickListener());
    }
    break;
   default:
    break;
   }
  }
 };


 


 

你可能感兴趣的:(Android错误)