绑定数据报错 The specified child already has a parent. You must call remove
来源:互联网 发布:pp助手mac 编辑:程序博客网 时间:2024/05/17 20:00
ViewPager 绑定数据报错Java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child’s parent first
参考资料:
http://blog.sina.com.cn/s/blog_b28d17530101ee5z.html
http://blog.csdn.net/mengliluohua_151/article/details/41866137
https://stackoverflow.com/questions/22936886/java-lang-illegalstateexception-while-using-viewpager-in-android
源代码:
ViewPager 使用代码:
views = new ArrayList<View>();views.add(gridView1);views.add(gridView2);views.add(gridView3);views.add(gridView4);views.add(gridView5);views.add(gridView6);//自定义适配器adapter = new MyPagerAdapter(views);vp.setAdapter(adapter);
布局代码:
<LinearLayout ......><android.support.v4.view.ViewPager android:id="@+id/view_pager" ......> <GridView android:id="@+id/gv_menu1" ......><GridView android:id="@+id/gv_menu2" ......><GridView android:id="@+id/gv_menu3" ......><GridView android:id="@+id/gv_menu4" ......><GridView android:id="@+id/gv_menu5" ......><GridView android:id="@+id/gv_menu6" ......> </GridView></android.support.v4.view.ViewPager></LinearLayout>
自定义adapter代码:
class MyPagerAdapter extends PagerAdapter { private List<View> viewList; public MyPagerAdapter(List<View> viewList) { this.viewList = viewList; } @Override public int getCount() { return viewList!=null ? viewList.size() : 0; } @Override public boolean isViewFromObject(View arg0, Object arg1) { // TODO Auto-generated method stub return arg0 == arg1; } @Override public Object instantiateItem(ViewGroup container, int position) { container.addView(viewList.get(position));//报错 return viewList.get(position); } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView(viewList.get(position)); } }
报错信息:
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child’s parent first.
原因调查:
类MyPagerAdapter 中的instantiateItem函数中,参数ViewGroup传入的是ViewPager对象,但ViewPager已经在布局中包含了这6个GridView对象,再去调用addView()去添加6个GridView中的一个,就会报错。
所以要添加需要先移除这个GridView,问题处理。
修改后代码:
public Object instantiateItem(ViewGroup container, int position) { if (container != null) { container.removeView(viewList.get(position)); } container.addView(viewList.get(position)); return viewList.get(position);}
应该不是最完善的解决之法,请多指正!
阅读全文
0 0
- 绑定数据报错 The specified child already has a parent. You must call remove
- 报错:The specified child already has a parent. You must call removeView
- . 报错 specified child already has a parent. You must call removeView() on the child's parent first
- 替换Fragment 报错 The specified child already has a parent. You must call removeView() on the child's pa
- 总是报The specified child already has a parent. You must call removeView() on the child's parent first.
- Android使用自定义对话框报错:The specified child already has a parent. You must call removeView() on the...
- Android使用自定义对话框报错:The specified child already has a parent. You must call removeView() on the...
- Android使用自定义对话框报错:The specified child already has a parent. You must call removeView() on the...
- Android自定义Dialog二次调用报错解决方法:The specified child already has a parent. You must call removeView()
- Android AlertDialog嵌套及二调用报错解决方法:The specified child already has a parent. You must call removeView()
- 自定义Dialog,第二次点击item报The specified child already has a parent. You must call removeView() on the chil
- 关于Dialog 俩次点击报错 :The specified child already has a parent. You must call removeView() on the child's
- The specified child already has a parent. You must call removeView() on the chil
- The specified child already has a parent. You must call removeView() on the
- java.lang.IllegalStateException The specified child already has a parent. You must call removeView()
- java.lang.IllegalStateException: The specified child already has a parent. You must call removeView
- java.lang.IllegalStateException The specified child already has a parent. You must call removeView()
- 异常-The specified child already has a parent. You must call removeView
- 使用适当的智能指针优化下面的程序
- javaScript对象
- 十分钟理解Java中的动态代理----强烈推荐
- 史上最全canOpenURL: failed问题解决办法
- 线性表的顺序存储和链式存储差异
- 绑定数据报错 The specified child already has a parent. You must call remove
- 算法设计作业17
- 欢迎使用CSDN-markdown编辑器
- IOS Swift3.0 获取导航栏跟状态栏的高度
- 天上掉Pizza
- Spark学习笔记(一)——spark简介
- 使用AJAX完成用户名是否存在异步校验:
- 用9kw网站搞定验证码
- excel表格右上角的红色小三角标志