在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. Jstl简单应用

    jsp引入信息------ <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" % ...

  2. vs2010 打包 SQL server compact 4.0 驱动程序

    sqlce 3.5应该是.net3.5环境下的.不知道最初的时候数据库的创建是用的3.5还是4.0 .这两天测试的时候,将4.0卸载了.就运行不上.报错为“未能加载文件或程序集“System.Data ...

  3. python3+ 模块学习 之 subprocess

    subprocess 模块方法: call() check_call() check_output() 以上三个方法用于创建一个子进程,父进程等待子进程结束.若shell = true, 则shell ...

  4. JAVA 多线程和并发学习笔记(四)

    1. 多进程 实现并发最直接的方式是在操作系统级别使用进程,进程是运行在它自己的地址空间内的自包容的程序.多任务操作系统可以通过周期性地将CPU从一个进程切换到另一个进程,来实现同时运行多个进程. 尽 ...

  5. top 10 tipis on Logging in Java- Tutorial (翻译)

    开篇废话,就记得出国之前有一回腾讯面试,面试官说既然你都快要出国了,英语肯定挺不错的,那为什么不去翻译一些国外好的文章呢,我希望找一个能主动学习的人,一直受教.目前在实习,需要做大数据相关方向,都是挺 ...

  6. Linux CentOS 7通过yum命令安装Mono4.0.1

    前言 上一篇中提到的快照方式安装Mono,该方式并不稳定,需要做各种配置,各种修改才能与jexus搭配运行. 一.安装源 rpm --import "http://keyserver.ubu ...

  7. gdb汇编调试

    GDB调试汇编堆栈分析 代码: sudo apt-get install libc6-dev-i386命令安装所需库 进入之后先在main函数处设置一个断点,再run一下,使用disassemble指 ...

  8. cs11_adventure c++_lab1

    exercise1.cc #include <iostream> #include <vector> #include <stdlib.h> #include &l ...

  9. html5 定位 获得当前位置的经纬度

    if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(showPosition, showError, { // ...

  10. CS0016: 未能写入输出文件“c:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files 解决方案

    1 设置c:windows\temp 目录访问权限 temp--> 属性-->安全-- > 添加network service -->并赋予其权限为 读 和 写--> 确 ...