java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
在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:
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.的更多相关文章
- 关于viewpager 里嵌套 listview 同时实现翻页功能的“java.lang.IllegalStateException: The specified child..."异常处理
这几天做项目用到了ViewPager,因为它可以实现左右划动多个页面的效果,然后 再每个页面里使用ListView,运行时总是出现”PagerAdapter java.lang.IllegalStat ...
- 安卓java.lang.IllegalStateException: The specified child already has a parent.解决方案
在使用ViewPager的时候遇到一个错误java.lang.IllegalStateException: The specified child already has a parent. You ...
- 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 ...
- 异常java.lang.IllegalStateException的解决
在初始化viewPagerAdapter时,显示异常.从网上找了找有两类这样的问题,一种是说给一个视图设置了两个父类,如: TextView tv = new TextView();layout.ad ...
- Tomcat部署项目时出错java.lang.IllegalStateException: ContainerBase.addChild: start:org.apache.catalina.Life
Tomcat部署项目时出错java.lang.IllegalStateException: ContainerBase.addChild: start:org.apache.catalina.Life ...
- 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 双击启动失败, ...
- 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 ...
- 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 ...
- java.lang.IllegalStateException: getOutputStream() has already been called for this response
ERROR [Engine] StandardWrapperValve[jsp]: Servlet.service() for servlet jsp threw exceptionjava.lang ...
随机推荐
- Sprintf新解 (ZT)
Sprintf新解 2012-08-06 11:26:45 分类: 原文地址:Sprintf新解 作者:harserm 由于sprintf 跟printf 在用法上几乎一样,只是打印的目的地不同而已 ...
- Oracle11g导出空表
# Oracle11g导出空表 <!-- create time: 2015-06-01 23:35:24 --> ###原因 11G中有个新特性,当表无数据时,不分配`segment`, ...
- 在VMware下正确克隆CentOS6.5的打开方式
引言 想必用VMware Workstation软件安装虚拟机,作为一个爱"折腾"的攻城狮肯定是千千万万遍的事情.无论是学习还是工作之中,我们都会遇到需要在一台物理主机上运行多台虚 ...
- 填坑*** WARNING L15: MULTIPLE CALL TO SEGMENT
填坑*** WARNING L15: MULTIPLE CALL TO SEGMENT 警告:发生了重入! 解释:在主循环里调用了一个函数,而在中断服务中又一次调用了同样的函数.当主循环运行到该函数中 ...
- 使用JQuery Ajax请求,在Controller里获取Session
昨天在做项目的时候,两个平台之间的切换,虽然两个网站的Session都指向了同一台机子,但是通过Ajax方式来请求时,就是不能获取到Session的值. 在调试的过程中发现,原来是Session的Is ...
- JVM调优
堆大小设置JVM 中最大堆大小有三方面限制:相关操作系统的数据模型(32-bt还是64-bit)限制;系统的可用虚拟内存限制;系统的可用物理内存限制.32位系统 下,一般限制在1.5G~2G;64为操 ...
- 如何在java中拟合正态分布
前言 最近在工作中需要拟合高斯曲线,在python中可以使用 scipy,相关代码如下: #!/usr/bin/env python # -*- coding=utf-8 -*- %matplotli ...
- jquery实现章节目录效果
<html><head><title>jquery实现章节目录效果</title> <script type="text/javascr ...
- python学习心得第五章
python学习心得第五章 1.冒泡排序: 冒泡是一种基础的算法,通过这算法可以将一堆值进行有效的排列,可以是从大到小,可以从小到大,条件是任意给出的. 冒泡的原理: 将需要比较的数(n个)有序的两个 ...
- calculator
#include <stdio.h> #include <stdlib.h> typedef enum { FALSE = , TRUE }BOOL; void calcula ...