在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. SQL语句学习手册实例版

    SQL语句学习手册实例版 表操作 例1  对于表的教学管理数据库中的表 STUDENTS ,可以定义如下: CREATE  TABLE  STUDENTS (SNO      NUMERIC (6, ...

  2. load mainaccount

    static void LoadMainAccountCSV(Args _args) { /* SysExcelApplication        excel; SysExcelWorkbooks  ...

  3. (windows)mongoDB3X+Robomongo的安装与基础配置

    一开始安装的时候还觉得很简单真正装了时候发现网上的资料参差不齐再次整理一份安装方法 ---------------------------------------------------------- ...

  4. javascript-with()方法

    1)简要说明         with 语句可以方便地用来引用某个特定对象中已有的属性,但是不能用来给对象添加属性.要给对象创建新的属性,必须明确地引用该对象. 2)语法格式  with(object ...

  5. ThinkPad W520 在 Windows Server 2012 / R2 中安装驱动

    1.安装Intel Chipset Device Software (INF Update Utility).2.安装ThinkPad ACPI电源管理驱动.3.安装电源管理软件.4.安装英特尔核芯显 ...

  6. JavaScript中的分支结构

    说到JavaScript中的分支结构,我们就不得不提到流程控制这个词,我们所有的程序都是由数据和算法组成的.程序=数据+算法通常我们所说的算法都可以通过"顺序","分支& ...

  7. dedecms讲解-arc.listview.class.php分析,列表页展示

    ./plus/list.php - 动态展示栏目列表页(也可能是频道封面)arc.listview.class.php 是dedecms的列表页的相关处理类__construct()         ...

  8. collection集合框架

    Java类集框架的优势:       1) 这种框架是高性能的.对基本类集(动态数组,链接表,树和散列表)的实现是高效率的.一般很少需要人工去对这些“数据引擎”编写代码.        2) 框架允许 ...

  9. JS定时程序,设定一个一直刷新,又时间间隔的js,读取当前的时间并显示

    <!doctype html><html><head><meta charset="utf-8"><title>无标题文 ...

  10. php web 信息采集

    <?php /** * 可以灵活配置使用的采集器 * 作者:Rain * 创建时间:2015-02-03 15:17:30 * 版本信息:V1.0 */ //////////////////// ...