最近遇到一个很让人头疼的问题,使用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()方法,以至于放弃了这种方法,以后要多思考,想到的解决办法如果完全不是自己想要的结果,一定要再检查一遍,很有可能是某个小地方没注意。

Android IllegalStateException: The specified child already has a parent问题解决办法的更多相关文章

  1. 安卓java.lang.IllegalStateException: The specified child already has a parent.解决方案

    在使用ViewPager的时候遇到一个错误java.lang.IllegalStateException: The specified child already has a parent. You ...

  2. 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 removeVi ...

  3. java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

    在ViewPager中,用Fragment显示页面时,报错: java.lang.IllegalStateException: The specified child already has a pa ...

  4. android之Android中的SQL查询语句LIKE绑定参数问题解决办法(sqlite数据库)

    由于考虑到数据库的安全性,不被轻易SQL注入,执行查询语句时,一般不使用直接拼接的语句,而是使用参数传递的方法.然后在使用参数传递的方法中时,发现当使用like方式查询数据时,很容易出现一个问题. 错 ...

  5. 关于viewpager 里嵌套 listview 同时实现翻页功能的“java.lang.IllegalStateException: The specified child..."异常处理

    这几天做项目用到了ViewPager,因为它可以实现左右划动多个页面的效果,然后 再每个页面里使用ListView,运行时总是出现”PagerAdapter java.lang.IllegalStat ...

  6. 【Android异常】The specified child already has a parent. You must call removeView() on the child's parent first.

    错误信息: Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must ...

  7. The specified child already has a parent错误

    10-05 23:39:48.187: E/AndroidRuntime(12854): Caused by: java.lang.IllegalStateException: The specifi ...

  8. 怎样实现动态加入布局文件(避免 The specified child already has a parent的问题)

    首先扯点别的:我应经连续上了两个星期的班了,今天星期一.是第三个周.这个班上的也是没谁了.近期老是腰疼. 预计是累了.近期也没跑步.今天下班继续跑起. 这篇文章讲一讲怎样在一个布局文件里动态加在一个布 ...

  9. bug_ _fragment_“The specified child already has a parent. You must call removeView"的解决以及产生的原因

    这个异常的出现往往是因为非法使用了某些方法引起的. 从字面意思上是说这个特定的child已经有一个parent了,你必须在这个parent中首先调用removeView()方法,才能继续你的内容.这里 ...

随机推荐

  1. Json(2)-DataContractJsonSerializer

    public static void DataContractSerializeDemo()        {            User user = new User { UserID = 1 ...

  2. Game Tutorials

    SDL: http://www.sdltutorials.com/tutorials        http://lazyfoo.net/ http://panda3d.noie.name/ http ...

  3. Boost的自动链接功能

    Boost是一个强大的C++第三方库,但是Boost的各种问题实在是很让人蛋疼.我搜到过一篇文章关于LuaBind使用Boost Build管理工具来管理源代码以及编译的博文,其第一句话就是Fuck ...

  4. 再也不要说,jquery动画呆板了

    1 show()方法和hide()方法 $("selector").show()  从display:none还原元素默认或已设置的display属性$("selecto ...

  5. ThinkPHP中PATHINFO模式优化

    ThinkPHP 3.1.2官方手册 第16.2章节 <隐藏index.php>中提到在Ngnix中隐藏index.php实现SEO友好的方法,其中使用了如下的代码 location / ...

  6. 如何正确理解深度学习(Deep Learning)的概念

    现在深度学习在机器学习领域是一个很热的概念,不过经过各种媒体的转载播报,这个概念也逐渐变得有些神话的感觉:例如,人们可能认为,深度学习是一种能够模拟出人脑的神经结构的机器学习方式,从而能够让计算机具有 ...

  7. 【Go】为什么用go; Golang Erlang 前世今生

    给自己一条退路,再次比较Erlang和Golang 2014-6-28 陈叶皓 chenyehao@gmail.com 雨天的周末,适合码字的时节... 一年前我开始学习go语言的时候,如获至宝,既有 ...

  8. google calendar

    1. user guide on google https://developers.google.com/google-apps/calendar/instantiate 2. google app ...

  9. c#之委托总结

    1.委托的声明和编译原理 声明委托: delegate void Translate(string str); 通过反编译之后可得下面代码 private sealed class Translate ...

  10. 3157: 国王奇遇记 & 3516: 国王奇遇记加强版 - BZOJ

    果然我数学不行啊,题解君: http://www.cnblogs.com/zhuohan123/p/3726933.html const h=; var fac,facinv,powm,s:..]of ...