`
wb284551926
  • 浏览: 555360 次
文章分类
社区版块
存档分类
最新评论

IllegalStateException: The specified child already has a parent问题解决办法(转载)

 
阅读更多

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

 

原帖地址:http://blog.csdn.net/mengliluohua_151/article/details/41866137

分享到:
评论

相关推荐

    关于IllegalStateException: The specified child already has a parent.

    IllegalStateException: The specified child already has a parent.我的博客中有文章讲解

    java.lang.IllegalStateException: getOutputStream() has already解决办法

    纠结了半天的 java.lang.IllegalStateException: getOutputStream() has already。这个问题困扰了半天,在网上查阅了大量资料 出这个错误一般就是下面2个.....

    java.lang.IllegalStateException: Cannot call sendError() after the response has

    在Java的Web开发中,`java.lang.IllegalStateException: Cannot call sendError() after the response has been committed` 是一个常见的错误,通常发生在尝试在HTTP响应已经发送到客户端之后调用`sendError()`方法...

    java.lang.IllegalStateException: OutputStream already obtain

    标题 "java.lang.IllegalStateException: OutputStream already obtain" 涉及到的是Java编程中的一个常见错误,特别是当处理I/O流时。这个异常通常在尝试获取已经存在的OutputStream实例时抛出,表明该输出流已经被...

    Java异常之—-Caused by: java.lang.IllegalStateException: Method has too many Body parameters

    异常:Caused by: java.lang.IllegalStateException: Method has too many Body parameters Caused by: java.lang.IllegalStateException: Method has too many Body parameters: public abstract ...

    ListView后台更新报错:java.lang.IllegalStateException

    java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but...

    Android异常 java.lang.IllegalStateException解决方法

    1. `java.lang.IllegalStateException: No wrapped connection.` 这个异常通常出现在尝试使用一个已经关闭或者无效的网络连接时。在多线程环境中,如果多个线程共享一个HTTP客户端实例,并且没有正确管理连接,就...

    spring入门好教程

    它的目标不仅仅是解决单一层次的问题,而是关注整个应用的生命周期,旨在消除中间层的“粘合”代码,让开发者能够更加专注于业务逻辑。 Spring的核心理念是控制反转(Inversion of Control,简称IoC)和面向切面...

    action-buffer:提供了在处理后台任务时避免片段过渡期间出现 IllegalStateException 的示例

    这个存储库提供了一种在处理片段传输和后台任务时避免“java.lang.IllegalStateException:Can not perform this action after onSaveInstanceState”的方法。 您可以在的非常权威的阅读有关该问题和可能的解决方案...

    powerbuilder

    ### PowerBuilder 中从 Excel 导入数据到 DataWindow 的实现方法 #### 标题解析: 在 PowerBuilder 开发环境中,经常需要将外部数据源(如 Excel 文件)中的数据导入到程序内部的数据展示组件(DataWindow)中进行...

    尚学堂android开发常用错误集.docx

    The specified child already has a parent. You must call removeView() on the child's parent first. ``` **原因分析**: 尝试将一个已经存在于父视图中的子视图添加到另一个父视图中。在进行添加操作前,需要先...

    IllegalStateException解决办法.md

    IllegalStateException解决办法.md

    android开发常用错误集归类.pdf

    如果尝试将一个Fragment添加到已有一个父视图的FrameLayout,会抛出异常:“The specified child already has a parent.” 解决方案是在`onCreateView()`中使用`inflater.inflate()`时传入`false`作为第三个参数,...

    Android ListView出现异常解决办法

    Android ListView 异常解决办法: ...10-26 18:30:45.085: E/AndroidRuntime(7323): java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. M

    nexus-3.70.1-02-java17-unix.tar.gz

    java.lang.IllegalStateException: The maximum Java version for OrientDb is Java 11. Please check current Java version meets this requirement. 参见: ...

    xfire访问webservice客户端用到的jar包

    在Java开发中,Web服务(Web Service)是一种标准的接口,允许不同系统之间进行通信,而XFire是早年流行的一款用于构建和消费Web服务的开源框架。它使用SOAP(简单对象访问协议)和XML(可扩展标记语言)作为基础,...

    java dubbo admin 2.5.3 + jdk1.7

    java dubbo admin 2.5.3 + jdk 1.7 亲试 好用 ... java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the ApplicationContext

    Jnative1.4_java调用动态库所需jar

    native int add(int a, int b); static { System.loadLibrary("mylib"); // 加载动态库 } } ``` `add`方法是本地方法,`System.loadLibrary`用于加载名为`mylib`的动态库。 3. **编译JNI头文件**: 使用`...

    oop object , for apple ios developer

    ### 面向对象编程与Objective-C:苹果iOS开发者指南 #### 一、概述 面向对象编程(Object-Oriented Programming, ...深入理解Objective-C不仅可以帮助开发者更好地理解苹果平台的技术栈,还能提升其解决问题的能力。

Global site tag (gtag.js) - Google Analytics