在ViewPager中,用Fragment显示页面时,报错:

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

意思是:一个视图只能有一个父容器,必须先移除其他的父容器。

解决方法:

在使用inflate()方法加载fragment布局资源的时候,不将视图存放在ViewGroup容器中,这样在后续的ViewPager中使用Fragment时,就不会报错已经存在容器了。

在Fragment的 onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)方法中,将

    View view=inflater.inflate(R.layout.fragment_language, container);

改为:

    View view=inflater.inflate(R.layout.fragment_language, container,false);

或者

    View view=inflater.inflate(R.layout.fragment_language, null);

为了便于理解,可以查看官网的api。inflate()方法的api如下所示:

第一种:

inflate(int resource, ViewGroup root, boolean attachToRoot)

Inflate a new view hierarchy from the specified xml resource.

第二种:

inflate(int resource, ViewGroup root)

Inflate a new view hierarchy from the specified xml resource.

更详细的解决方案,参见StackOverflow:

http://stackoverflow.com/questions/13559353/how-to-solve-for-viewpager-the-specified-child-already-has-a-parent-you-must

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.的更多相关文章

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

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

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

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

  3. 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 ...

  4. 异常java.lang.IllegalStateException的解决

    在初始化viewPagerAdapter时,显示异常.从网上找了找有两类这样的问题,一种是说给一个视图设置了两个父类,如: TextView tv = new TextView();layout.ad ...

  5. Tomcat部署项目时出错java.lang.IllegalStateException: ContainerBase.addChild: start:org.apache.catalina.Life

    Tomcat部署项目时出错java.lang.IllegalStateException: ContainerBase.addChild: start:org.apache.catalina.Life ...

  6. myeclipse 无法启动 java.lang.IllegalStateException: Unable to acquire application service. Ensure that the org.eclipse.core.runtime bundle is resolved and started (see config.ini).

    把myeclipse10 按照目录完整拷贝到了另外一台电脑, 另外的目录 原安装目录 D\:\soft\i\myeclipse10 新安装目录 E\:\soft\myeclipse10 双击启动失败, ...

  7. java.lang.IllegalStateException:Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.

    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xxx...}: java.lang.IllegalSta ...

  8. java.lang.IllegalStateException: Not allowed to create transaction on shared EntityManager - use Spring transactions or EJB CMT instead

    java.lang.IllegalStateException: Not allowed to create transaction on sharedEntityManager - use Spri ...

  9. java.lang.IllegalStateException: getOutputStream() has already been called for this response

    ERROR [Engine] StandardWrapperValve[jsp]: Servlet.service() for servlet jsp threw exceptionjava.lang ...

随机推荐

  1. sftp自动授权登录

    客户的账号下执行 ssh-keygen -t rsa 生成秘钥文件 ~/.ssh/id_rsa --秘钥文件 ~/.ssh/id_rsa.pub --公钥文件 将公钥文件id_rsa.pub放到sft ...

  2. 第一章 Andorid系统移植与驱动开发概述 - 读书笔记

    Android驱动月考1 第一章 Andorid系统移植与驱动开发概述 - 读书笔记 1.Android系统的架构: (1)Linux内核,Android是基于Linux内核的操作系统,并且开源,所以 ...

  3. MySQL 基础语句

    MySQL 基础语句 多个知识点 ----------------------------------------------------------------------------------- ...

  4. Xenko基础API笔记2-手势

    交互: Drag Gesture Type : Continuous Configuration class: GestureConfigDrag Event class: GestureEventD ...

  5. jmeter上传文件搞了一天,才搞定,没高人帮忙效率就是低,赶紧记下来,以备后用

    先用谷歌浏览器抓包,抓到的包类似这样: 在jmeter里添加一个http请求,配置好参数,方法,端口,路径等, 勾选 在“同请求一起发送参数”里填写上面抓包的部分数据: 分别对应录入,勾选“编码” 我 ...

  6. JUC.Condition学习笔记[附详细源码解析]

    目录 Condition的概念 大体实现流程 I.初始化状态 II.await()操作 III.signal()操作 3个主要方法 Condition的数据结构 线程何时阻塞和释放 await()方法 ...

  7. C语言Notebook

    int *pointer=NULL    /*指针变量一定要赋初值*/pritnf("Pointer' address is:%p",&pointer);  /*打印指针变 ...

  8. Fisher vector for image classification

    http://files.cnblogs.com/files/sylar120/fisher_vector.rar 拿各个参数上的偏导作为特征

  9. IE8下标签float导致的bug。

    前几天帮朋友写一个页面,今天在IE8下面发现一个很奇葩的问题,给a标签添加了float:left之后a标签内的图片不显示了,去掉float:left之后就能正常显示. 代码: <!DOCTYPE ...

  10. android常用调试工具fiddle、wireshark和android studio的配置

    Fiddle配置android代理 在wifi的同一个局域网环境的windows主机中安装fiddler,并且启动,如本次192.168.3.14 在android手机端配置代理为该主机 还有一种方式 ...