还是由于一个类中使用xutils进行联网请求,开启了两个线程,导致在得到请求数据之前viewPagerAdapter就已经setAdapter了,setAdapter时getCount是0,但是数据加载完成后,adapter并没有notify,导致异常。确实是由于数据改变了却没有更新adapter。
在构造器中,request()联网请求数据开启了分线程,setAdapter在主线程中直接执行。在viewsDetail加载完成后adapter.notify重写加载就可以了
private final PagerAdapter adapter = new PagerAdapter() { @Override public Object instantiateItem(ViewGroup container, int position) { View view = viewsDetail.get(position); container.addView(view); return view; } @Override public void destroyItem(ViewGroup container, int position, Object object) { vp_news_pager.removeView((View) object); } @Override public int getCount() { LogUtils.e("TAG" + viewsDetail.size()); return viewsDetail.size(); } @Override public boolean isViewFromObject(View view, Object object) { return view == object; } }; private ArrayList<String> newsDetailTitles; private List<NewsBean.DataEntity.ChildrenEntity> children; public NewsPager(Context context) { activity = (Activity) context; view = View.inflate(activity, R.layout.news_pager, null); x.view().inject(this, view); title_pager = (TextView) view.findViewById(R.id.title_pager); ib_base_pager = (ImageButton) view.findViewById(R.id.ib_base_pager); ib_base_pager.setVisibility(View.VISIBLE); ib_base_pager.setOnClickListener(onClickListener); title_pager.setText("新闻"); request(); vp_news_pager.setAdapter(adapter); } private void initViewsDetail() { LogUtils.e("传输"); NewsDetailPager newsDetailPager = new NewsDetailPager(activity, children); View view = newsDetailPager.initView(); viewsDetail.add(view); TopicDetailPager topicDetailPager = new TopicDetailPager(activity); viewsDetail.add(topicDetailPager.initView()); PhotosDetailPager photosDetailPager = new PhotosDetailPager(activity); viewsDetail.add(photosDetailPager.initView()); InteractDetailPager interactDetailPager = new InteractDetailPager(activity); viewsDetail.add(interactDetailPager.initView()); LogUtils.e("传输完成"); } private NewsBean newsBean; private void request() { RequestParams entity = new RequestParams(ContentUtils.REQUEST); Callback.CommonCallback<? extends Object> callback = new Callback.CommonCallback<String>() { @Override public void onSuccess(String result) { LogUtils.e("request" + result); Gson gson = new Gson(); newsBean = gson.fromJson(result, NewsBean.class); String title = newsBean.getData().get(0).getChildren().get(0).getTitle(); LogUtils.e("title=========" + title); setLeftData(newsBean); getNewsDetailData(newsBean); } @Override public void onError(Throwable ex, boolean isOnCallback) { LogUtils.e("request" + ex); } @Override public void onCancelled(CancelledException cex) { } @Override public void onFinished() { LogUtils.e("request"); } }; x.http().get(entity, callback); } private void getNewsDetailData(NewsBean newsBean) { children = newsBean.getData().get(0).getChildren(); int size = children.size(); newsDetailTitles = new ArrayList<>(); for (int i = 0; i < size; i++) { newsDetailTitles.add(children.get(i).getTitle()); LogUtils.e("title===" + children.get(i).getTitle() + size); } initViewsDetail(); adapter.notifyDataSetChanged(); }