在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. Unix 用gdb分析core dump文件

    产生core文件条件 用ulimit -c 指定core文件大小来开启core文件的生成,如:ulimit -c unlimited 用gdb分析core文件的条件 可执行程序在编译时,需加入-g参数 ...

  2. Modelsim-altera 仿真 顶层原理图的解决办法

    解决办法:首先需要将.bdf原理图文件转换为Verilog HDL等第三方EDA工具所支持的标准描述文件.在Quartus下,保持*.bdf为活动窗口状态,运行[File]/[Create/Updat ...

  3. 多个DLL合并成一个DLL

    多个DLL合并成一个DLL,把DLL合并到Exe中的解决方案 1:) 下载 http://download.microsoft.com/download/1/3/4/1347C99E-9DFB-425 ...

  4. js prepend() 和append()区别

    prepend() 方法在被选元素的开头(仍位于内部)插入指定内容.prepend() 语法:$(selector).prepend(content) 或 $(selector).prepend(fu ...

  5. 官方提供的屏蔽百度转码Baidu Transcoder的方法no-transform

    首先,百度在官方的声明中说:[喝小酒的网摘]http://blog.hehehehehe.cn/a/17112.htm百度仅作为中立的转码工具及相关技术的提供方.在转码过程中,百度对第三方网站内容不做 ...

  6. 采用TCP协议实现PIC18F97J60 ethernet bootloader

    了解更多关于bootloader 的C语言实现,请加我QQ: 1273623966 (验证信息请填 bootloader),欢迎咨询或定制bootloader(在线升级程序). TCP/IP Stac ...

  7. java学习第17天(TreeSet HashSet)

    Set集合的特点(与ArrayList相比) 无序,唯一 主要学习它的两个子类 HashSet集合 A:底层数据结构是哈希表(是一个元素为链表的数组) B:哈希表底层依赖两个方法:hashCode() ...

  8. RabbitMQ 入门指南(Java)

    RabbitMQ是一个受欢迎的消息代理,通常用于应用程序之间或者程序的不同组件之间通过消息来进行集成.本文简单介绍了如何使用 RabbitMQ,假定你已经配置好了rabbitmq服务器. Rabbit ...

  9. WCF之Binding详解

    本文的出发点: 通过阅读本文,您能了解以下知识: WCF中的Binding是什么? Binding的组 成? Binding Element 的分类? Binding描述 了那些层面的信息? 选择正确 ...

  10. Kafka深入理解-3:Kafka如何删除数据(日志)文件

    Kafka作为消息中间件,数据需要按照一定的规则删除,否则数据量太大会把集群存储空间占满. 参考:apache Kafka是如何实现删除数据文件(日志)的 Kafka删除数据有两种方式 按照时间,超过 ...