Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> viewPager使用時加載數據時顯示IllegalStateException異常,解決不了。。。。,viewpager取消預加載

viewPager使用時加載數據時顯示IllegalStateException異常,解決不了。。。。,viewpager取消預加載

編輯:關於android開發

viewPager使用時加載數據時顯示IllegalStateException異常,解決不了。。。。,viewpager取消預加載


從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 @Override 29 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 @Override 38 public void destroyItem(ViewGroup container, int position, Object object) { 39 LogUtils.e("TAG"); 40 container.removeView((View) object); 41 } 42 43 @Override 44 public int getCount() { 45 LogUtils.e("TAG" + newsDetailTitles.size()); 46 47 return newsDetailTitles.size(); 48 } 49 50 @Override 51 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)

 

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved