最近遇到一个很让人头疼的问题,使用viewpager动态添加页面或者删除页面时出现了问题(java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first),在stackoverflow上找到了解决办法。(http://stackoverflow.com/questions/22936886/java-lang-illegalstateexception-while-using-viewpager-in-android

原文是:

the problem is that in your adapters method instantiateItem
you call container.addView(v);

but every View can have only one parent,
so it can be added only one time to a container via addView(...).

When you open the popup the first time, everything works, because v
doesn't have a parent that time. But when you open your popupwinow the second time,
it adds the view again to the container. That cerates the error.

Try to destroy the view if you close the popup view or remove all children views from it with
container.removeAllViews()

解决办法是在instantiateItem中使用如下方式:

ViewGroup parent = (ViewGroup) v.getParent();

if (parent != null) {

parent.removeAllViews();

}

container.addView(v);
中间很多次尝试已经接近答案,但是习惯性的去把v.getParent()强制转化为view,view没有removeView()方法,以至于放弃了这种方法,以后要多思考,想到的解决办法如果完全不是自己想要的结果,一定要再检查一遍,很有可能是某个小地方没注意。

最新文章

  1. Request.Form接收不到post数据.
  2. 安卓自定义View(一)自定义控件属性
  3. Spring整合JAX-WS
  4. Error && MFC
  5. SQLServer分页存储过程
  6. js构造函数
  7. System Operations on AWS - Lab 6W - Using Auto Scaling (Windows)
  8. Xcode HeaderDoc 教程(2)
  9. 如何在Ubuntu 14.04中使用Samba共享文件
  10. Fiddler 教程(转)
  11. 计算机与ARM板通过路由器相连
  12. 最短路径算法—Dijkstra(迪杰斯特拉)算法分析与实现(C/C++)
  13. 开源解析器--ANTLR
  14. StringBuilder类中的重要方法
  15. docker storage driver
  16. 初学HTML-9
  17. WebApi表头设置跨域
  18. ICM Technex 2018 and Codeforces Round #463 (Div. 1 + Div. 2, combined) A
  19. Codeforces Round #506 (Div. 3) C. Maximal Intersection
  20. django-restful风格

热门文章

  1. TCP连接,传输数据时的粘包问题讨论
  2. Barracuda VS antelope
  3. C#获取数据库中的Instance
  4. hornetq 入门(1)
  5. .NET基础之:i++和i=i+1和++i的区别
  6. 清除SQL Server执行计划
  7. DispatcherServlet--Spring的前置控制器作用简介
  8. Win7超级终端查看单片机printf输出
  9. 【BZOJ】【2879】【NOI2012】美食节
  10. The service ‘xxx’ configured for WCF is not registered with the Autofac container