ViewPager onMeasure抛出错误 - 预期项目数高于找到的项目数

时间:2016-03-10 18:48:19

标签: android android-viewpager notifydatasetchanged android-pageradapter

我正在使用描述here的自定义WrapContentViewPager。我使用此自定义ViewPager来显示图像列表 - 每个图像都在与自定义对象关联的URL中找到(因此PagerAdapter接收自定义对象列表)。当我运行代码时,有时它工作正常,但有时,我得到以下错误:"应用程序的PagerAdapter更改了适配器的内容而不调用PagerAdapter#notifyDataSetChanged!预期的适配器项目数:5,找到:0。"没有押韵或理由何时发生,当它不发生时,它似乎是随机的。错误日志清楚地表明它与我的自定义ViewPager的 onMeasure()方法有关,但是根据原始帖子概述,此自定义ViewPager适用于其他所有人,因此我&#39 ; m怀疑错误是我的PagerAdapter中的其他地方。

关于 FragmentPagerAdapter ,已经提出了类似的问题(关于相同的错误),并且错误与addTab方法有关。请参阅herehere

但是,我的问题是 PagerAdapter 而不是 FragmentPagerAdapter ,它不使用addTab()方法。还有一个类似的问题涉及 PagerAdapter ,但从未收到过答案 - 请参阅here。这是日志:

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)

这是我的自定义 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);
}
}

这是我的 PagerAdapter 代码。

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);
}

最后,我的Activity使用ViewPager和PagerAdapter。

ImagePagerAdapter imagePagerAdapter = new ImagePagerAdapter(this, promotedClasses, width);
    View Pager imageViewPager = (ViewPager) findViewById(R.id.imageViewPager);
    imageViewPager.setAdapter(imagePagerAdapter);

任何帮助都会非常感激,我已经坚持了很长时间。

0 个答案:

没有答案