从newsPager中得到newsDetailTitles标题的详细内容,这是通过构造器传过来的。打印日志78行能打印,45行打印出来共size是12.但是程序出现了异常
java.lang.IllegalStateException:
The application's PagerAdapter changed the adapter's contents without calling PagerAdapter#notifyDataSetChanged。
从网上查询的结果是adapter中的数据改变了,却没有重新加载数据,需要在数据改变之后adapter.notifyDataSetChanged。但是找不到数据改变的地方。
今天太累了,这个bug找到答案时再写。
1 package com.atguigu.beijingnews2.pager.newsdetailpager; 2 3 import android.app.Activity; 4 import android.content.Context; 5 import android.support.v4.view.PagerAdapter; 6 import android.support.v4.view.ViewPager; 7 import android.view.View; 8 import android.view.ViewGroup; 9 import android.widget.TextView;10 11 import com.atguigu.beijingnews2.R;12 import com.atguigu.beijingnews2.utils.LogUtils;13 14 import org.xutils.view.annotation.ViewInject;15 import org.xutils.x;16 17 import java.util.ArrayList;18 19 /**20 * Created by Administrator on 2016/2/18.21 */22 public class NewsDetailPager {23 private Activity activity;24 @ViewInject(R.id.vp_news_detail_paper)25 private ViewPager vp_news_detail_paper;26 private ArrayList<String> newsDetailTitles=new ArrayList<>() ;27 private PagerAdapter adapter = new PagerAdapter() {28 @Override29 public Object instantiateItem(ViewGroup container, int position) {30 LogUtils.e("TAG");31 // String title = newsDetailTitles.get(position);32 // textView.setText(title);33 container.addView(views.get(position));34 return views.get(position);35 }36 37 @Override38 public void destroyItem(ViewGroup container, int position, Object object) {39 LogUtils.e("TAG");40 container.removeView((View) object);41 }42 43 @Override44 public int getCount() {45 LogUtils.e("TAG" + newsDetailTitles.size());46 47 return newsDetailTitles.size();48 }49 50 @Override51 public boolean isViewFromObject(View view, Object object) {52 LogUtils.e("TAG");53 return view == object;54 }55 };56 private TextView textView;57 ArrayList<View> views = new ArrayList<>();58 public NewsDetailPager(Context context, ArrayList<String> newsDetailTitles ) {59 activity = (Activity) context;60 this.newsDetailTitles=newsDetailTitles;61 // initView();62 63 64 }65 66 67 68 public View initView() {69 View view = View.inflate(activity, R.layout.news_detail_paper, null);70 x.view().inject(this, view);71 for (int i = 0; i < newsDetailTitles.size(); i++) {72 TextView textView = new TextView(activity);73 textView.setText(newsDetailTitles.get(i));74 views.add(textView);75 LogUtils.e("TAG");76 }77 // textView = new TextView(activity);78 LogUtils.e("TAG");79 vp_news_detail_paper.setAdapter(adapter);80 return view;81 }82 }
E/AndroidRuntime: FATAL EXCEPTION: main Process: com.atguigu.beijingnews2, PID: 1622 java.lang.IllegalStateException: The application's PagerAdapter changed the adapter's contents without calling PagerAdapter#notifyDataSetChanged! Expected adapter item count: 0, found: 4 Pager id: com.atguigu.beijingnews2:id/vp_news_pager Pager class: class com.atguigu.beijingnews2.view.NoScrollViewPager Problematic adapter: class com.atguigu.beijingnews2.pager.NewsPager$2 at android.support.v4.view.ViewPager.populate(ViewPager.java:999) at android.support.v4.view.ViewPager.populate(ViewPager.java:951) at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1473) at android.view.View.measure(View.java:16497) at android.widget.LinearLayout.measureVertical(LinearLayout.java:847) at android.widget.LinearLayout.onMeasure(LinearLayout.java:588) at android.view.View.measure(View.java:16497) at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1488) at android.view.View.measure(View.java:16497) at android.widget.LinearLayout.measureVertical(LinearLayout.java:847) at android.widget.LinearLayout.onMeasure(LinearLayout.java:588) at android.view.View.measure(View.java:16497) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125) at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) at android.view.View.measure(View.java:16497) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125) at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) at android.view.View.measure(View.java:16497)