編輯:關於android開發
從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)
Android項目從Eclipse增加支持Android Studio 使用Eclipse開發Android已經有些年頭了,然而Android Studio(後面簡稱
Android網絡編程(一)HTTP協議原理 前言 這篇文章是這個系列的開篇,作為移動開發者,開發的應用不免會對網絡進行訪問,雖然現在已經有很多的開源庫幫助我們可以輕而易
Android屏幕適配方案 1、概述 大家在Android開發時,肯定會覺得屏幕適配是個尤其痛苦的事,各種屏幕尺寸適配起來蛋疼無比。如果我們換個角度我們看下這個問題,不知
Docker+Redis3集群環境搭建Docker宿主機新建3個容器,三個容器分別作為redis集群的三個節點,三個master,三個slave!實驗目標:client通