I am using the custom WrapContentViewPager described here. I am using this custom ViewPager to display a list of images - each image is found at a url associated with a custom object (so the PagerAdapter receives the list of custom objects). When I run the code, sometimes it works fine, but other times, I get the following error: "The application's PagerAdapter changed the adapter's contents without calling PagerAdapter#notifyDataSetChanged! Expected adapter item count: 5, found: 0." There is no rhyme or reason to when it occurs and when it does not, it appears random. The error log makes it clear that it has to do with the onMeasure() method of my custom ViewPager, but this custom ViewPager works fine for everyone else according to the original post outlining it, so I'm suspicious the error is somewhere else in my PagerAdapter.
A similar question (about the same error) has been asked in relation to a FragmentPagerAdapter and the error had to do with the addTab method. See here and here.
However, my question is for a PagerAdapter rather than a FragmentPagerAdapter that does not use the addTab() method. A similar question has also been asked relating to a PagerAdapter on but never received an answer - see here. Here is the log:
AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.IllegalStateException: The application's PagerAdapter changed the adapter's contents without calling PagerAdapter#notifyDataSetChanged! Expected adapter item count: 5, found: 0 Pager id: com.testapp.www.test:id/imageViewPager Pager class: class com.testapp.www.test.WrapContentViewPager Problematic adapter: class com.testapp.www.test.ImagePagerAdapter
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 com.testapp.www.test.WrapContentViewPager.onMeasure(WrapContentViewPager.java:39)
at android.view.View.measure(View.java:17388)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5353)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:17388)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5353)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.support.v7.internal.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:124)
at android.view.View.measure(View.java:17388)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5353)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:17388)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5353)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:17388)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5353)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:17388)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5353)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2533)
at android.view.View.measure(View.java:17388)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2217)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1354)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1553)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1238)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6473)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:803)
at android.view.Choreographer.doCallbacks(Choreographer.java:603)
at android.view.Choreographer.doFrame(Choreographer.java:573)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:789)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
Here is my custom WrapContentViewPager:
public class WrapContentViewPager extends ViewPager {
BBActivity parentActivity;
public WrapContentViewPager(Context context) {
super(context);
parentActivity = (BBActivity) context;
}
public WrapContentViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
parentActivity = (BBActivity) context;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int height = 0;
for(int i = 0; i < getChildCount(); i++) {
View child = getChildAt(i);
child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
int h = child.getMeasuredHeight();
if(h > height) height = h;
}
heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
parentActivity.disableAutoScroll();
return super.onTouchEvent(event);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
parentActivity.disableAutoScroll();
return super.onInterceptTouchEvent(event);
}
}
Here is my PagerAdapter code.
public class ImagePagerAdapter extends PagerAdapter {
Context mContext;
LayoutInflater mLayoutInflater;
ArrayList<myObject> myObjectList;
int width;
public ImagePagerAdapter(Context context, ArrayList<myObject> myObjectListIn, int widthIn) {
mContext = context;
myObjectList = myObjectListIn;
width = widthIn;
mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return myObjectList.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
View itemView = mLayoutInflater.inflate(R.layout.image_view_pager_item, container, false);
ImageView imageView = (ImageView) itemView.findViewById(R.id.imageView);
Picasso.with(mContext).load(myObjectList.get(position).pictureLink).into(imageView);
container.addView(itemView);
return itemView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((RelativeLayout) object);
}
And last, my Activity using the ViewPager and PagerAdapter.
ImagePagerAdapter imagePagerAdapter = new ImagePagerAdapter(this, promotedClasses, width);
View Pager imageViewPager = (ViewPager) findViewById(R.id.imageViewPager);
imageViewPager.setAdapter(imagePagerAdapter);
Any help would be greatly appreciated, I've been stuck on this for a long time.