异常IllegalStateException终于解决了

还是由于一个类中使用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();
    }

 

转载于:https://www.cnblogs.com/chienhsu/p/5202525.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值