qq_30380777
SaeRan__
2015-09-29 03:09

求助各位大神LIstfragment中,分线程获取数据后刷新界面崩溃,用分线程加载数据后

5
  • exception
  • fragment
  • 代码
  • viewpager
如题:代码如下,要在viewpager中的一个fragment中加载一个listview,用分线程加载数据后,handleMessage给成员变量的集合赋值后,notify,会直接崩溃

/**

  • 报警图片
    */
    public class AlarmPhotoFragment extends ListFragment {

    private int mType = 0;
    private ListView mListView;
    public static int type = 0;
    private String urlStr;
    private LayoutInflater mInflater;
    private List cids = new ArrayList();
    List lists = new ArrayList();
    LocalRecordAlarmAdapter mLocalRecordAlarmAdapter;
    private AlarmPhotoBean alarmPhotoBeans = new AlarmPhotoBean();

    int page = 1;

    private Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
    super.handleMessage(msg);

        switch (msg.what) {
            case 9:
                String json = (String) msg.obj;
                Gson gson = new Gson();
                AlarmPhotoBean alarmPhotoBean = gson.fromJson(json, AlarmPhotoBean.class);
    
                CLog.v("alarmPhotoBean: " + alarmPhotoBean);
                if (!alarmPhotoBean.getBase_urls().isEmpty() && !alarmPhotoBean.getTimestamps().isEmpty()) {
                    List<AlarmPhotoBean.Timestamps> timestamps = alarmPhotoBean.getTimestamps();
                    List<AlarmPhotoBean.Timestamps.ListEntity> list = timestamps.get(0).getList();
    
                    CLog.v("alarmPhotoBeans:" + alarmPhotoBean);
                    lists.addAll(list);
                    alarmPhotoBeans = alarmPhotoBean;
                    com.lingyang.sdk.util.CLog.v("lists: " + lists);
                    notifyDataSetChanged();
                }
                break;
        }
    }
    

    };

    @Override
    public void onAttach(Activity activity) {
    super.onAttach(activity);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mType = getArguments().getInt("type", 0);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
    @Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    View view = inflater.inflate(R.layout.fragment_localrecord_alarm, container, false);

    initData();
    mInflater = inflater;
    page = 1;
    return view;
    

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    mListView = getListView();
    if(mLocalRecordAlarmAdapter == null)
    {
    mLocalRecordAlarmAdapter = new LocalRecordAlarmAdapter(getActivity(), lists, alarmPhotoBeans);
    } else {
    setListAdapter(mLocalRecordAlarmAdapter);
    mLocalRecordAlarmAdapter.notifyDataSetChanged();
    }
    }

    private void notifyDataSetChanged() {
    if (mLocalRecordAlarmAdapter == null) {
    mLocalRecordAlarmAdapter = new LocalRecordAlarmAdapter(getActivity(), lists, alarmPhotoBeans);
    }
    CLog.v("notifyDataSetChanged()");
    setListAdapter(mLocalRecordAlarmAdapter);
    }

    /**

    • 初始化集合信息
      */
      private void initData() {
      Date dNow = new Date(); //当前时间
      long time = dNow.getTime();
      long begoreTime = TimeUtils.getBeforeTimeStamp(time);

      getTimeStamp(begoreTime, time);
      }

    public void getTimeStamp(long start, long end) {
    String userToken;
    String accessToken;
    List mListCamera = AttentionFragment.mListCamera;
    // for (final CameraResponse.MyCameras.Camera cameras : mListCamera) {
    final CameraResponse.MyCameras.Camera cameras = mListCamera.get(0);

        com.lingyang.sdk.util.CLog.v("cid: " + cameras.cid);
        userToken = CloudOpenAPI.getInstance().getUserToken();
        accessToken = CloudOpenAPI.getInstance().getDeviceToken(cameras.cid);
        urlStr = AlarmHttpUtils.TOTIMESTAMP + cameras.cid + "/snapshots?from=" + start / 1000 + "&type=3" + "&to=" + end / 1000
                + "&user_token=" + userToken + "&access_token=" + accessToken;
    
        cids.add(cameras.cid);
    
        Object obj = new Object();
        synchronized (obj) {
            new Thread() {
                public void run() {
                    try {
                        URL url = null;
                        HttpURLConnection conn = null;
                        InputStream is = null;
                        ByteArrayOutputStream baos = null;
                        try {
                            url = new URL(urlStr);
                            conn = (HttpURLConnection) url.openConnection();
                            conn.setReadTimeout(AlarmHttpUtils.TIMEOUT_IN_MILLIONS);
                            conn.setConnectTimeout(AlarmHttpUtils.TIMEOUT_IN_MILLIONS);
                            conn.setRequestMethod("GET");
                            conn.setRequestProperty("accept", "*/*");
                            conn.setRequestProperty("connection", "Keep-Alive");
                            com.lingyang.sdk.util.CLog.v("conn.getResponseCode(): " + conn.getResponseCode());
                            if (conn.getResponseCode() == 200) {
                                is = conn.getInputStream();
                                baos = new ByteArrayOutputStream();
                                int len = -1;
                                byte[] buf = new byte[128];
    
                                while ((len = is.read(buf)) != -1) {
                                    baos.write(buf, 0, len);
                                }
                                baos.flush();
                                baos.toString();
    
                                Message message = new Message();
                                message.what = 9;
                                message.obj = baos.toString();
                                handler.sendMessage(message);
                            } else {
                                throw new RuntimeException(" responseCode is not 200 ... ");
                            }
    
                        } catch (Exception e) {
                            e.printStackTrace();
                        } finally {
                            try {
                                if (is != null)
                                    is.close();
                            } catch (IOException e) {
                            }
                            try {
                                if (baos != null)
                                    baos.close();
                            } catch (IOException e) {
                            }
                            conn.disconnect();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }.start();
        }
    

    // }
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    }
    @Override
    public void onResume() {
    super.onResume();

    }

}

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答