Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

当view数量少于4个时,滑动抛出异常。 #3

Closed
jiandao7114 opened this issue May 25, 2015 · 5 comments
Closed

当view数量少于4个时,滑动抛出异常。 #3

jiandao7114 opened this issue May 25, 2015 · 5 comments

Comments

@jiandao7114
Copy link

当view数量少于4个时,滑动抛出异常。

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
at android.view.ViewGroup.addViewInner(ViewGroup.java:3562)
at android.view.ViewGroup.addView(ViewGroup.java:3415)
at android.support.v4.view.ViewPager.addView(ViewPager.java:1309)
at android.view.ViewGroup.addView(ViewGroup.java:3360)
at android.view.ViewGroup.addView(ViewGroup.java:3336)
at cn.bingoogolapple.bgabanner.BGABanner$MyAdapter.instantiateItem(BGABanner.java:262)

@bingoogolapple
Copy link
Owner

当view数量少于3个时一定会报错,刚好等于3个时,如果滑动的速度非常快也会有这个问题,除非用户太无聊死劲滑动

@jiandao7114
Copy link
Author

@Override
        public Object instantiateItem(ViewGroup container, int position) {
            // container容器就是ViewPager, position指的是ViewPager的索引
            // 从View集合中获取对应索引的元素, 并添加到ViewPager中
            if (mAutoPlayAble) {
                //对ViewPager页号求模取出View列表中要显示的项
                position %= mViews.size();
                if (position < 0) {
                    position = mViews.size() + position;
                }
            }
            View view = mViews.get(position);
            //如果View已经在之前添加到了一个父组件,则必须先remove,否则会抛出IllegalStateException。
            ViewParent vp = view.getParent();
            if (vp != null) {
                ViewGroup parent = (ViewGroup) vp;
                parent.removeView(view);
            }
            container.addView(view);
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            //Warning:不要在这里调用removeView
        }

@felixcc
Copy link

felixcc commented Jun 15, 2015

我添加了五个view( ImageView convertView = new ImageView(mContext);),为啥anr。必现。

@bingoogolapple
Copy link
Owner

@noblesky 从xml文件中加载ImageView,直接在代码里ImageView convertView = new ImageView(mContext)的话,设置下布局参数

@felixcc
Copy link

felixcc commented Jun 17, 2015

哥们你QQ多少?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants