The error "IllegalStateException" is displayed when data is loaded in viewPager, which cannot be solved... ". viewpager unpreloads
Obtain the details of the newsDetailTitles title from newsPager, which is passed by the constructor. 78 lines of printed logs can be printed, and 45 lines can be printed with a total size of 12. However, an exception occurs in the program.
Java. lang. IllegalStateException:
The application's PagerAdapter changed the adapter's contents without calling PagerAdapter # notifyDataSetChanged.
The online query result shows that the data in the adapter has changed, but the data has not been reloaded. The adapter must be updated after the data changes.NotifyDataSetChanged. However, the data cannot be changed.
I am so tired today that I will try again when I find the answer to this 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)