Activity动态添加Fragment时遇到的问题
1.Activity动态调用代码
TitleFragement a = new TitleFragement();
getFragmentManager().beginTransaction().add(R.id.tt, a)
.commit();
异常信息:
The specified child already has a parent.
经分析:
对应Fragment代码:
View view = inflater.inflate(R.layout.titlefragement, container);
应改成:
View view = inflater.inflate(R.layout.titlefragement, container, false);
查看定义:
/**
* Inflate a new view hierarchy from the specified xml resource. Throws
* {@link InflateException} if there is an error.
*
* @param resource ID for an XML layout resource to load (e.g.,
* <code>R.layout.main_page</code>)
* @param root Optional view to be the parent of the generated hierarchy.
* @return The root View of the inflated hierarchy. If root was supplied,
* this is the root View; otherwise it is the root of the inflated
* XML file.
*/
public View inflate(int resource, ViewGroup root) {
return inflate(resource, root, root != null);
}
public View inflate(int resource, ViewGroup root, boolean attachToRoot)
/**
* Inflate a new view hierarchy from the specified xml resource. Throws
* {@link InflateException} if there is an error.
*
* @param resource ID for an XML layout resource to load (e.g.,
* <code>R.layout.main_page</code>)
* @param root Optional view to be the parent of the generated hierarchy (if
* <em>attachToRoot</em> is true), or else simply an object that
* provides a set of LayoutParams values for root of the returned
* hierarchy (if <em>attachToRoot</em> is false.)
* @param attachToRoot Whether the inflated hierarchy should be attached to
* the root parameter? If false, root is only used to create the
* correct subclass of LayoutParams for the root view in the XML.
* @return The root View of the inflated hierarchy. If root was supplied and
* attachToRoot is true, this is root; otherwise it is the root of
* the inflated XML file.
*/
root:需要附加到resource资源文件的根控件,什么意思呢,就是inflate()会返回一个 View对象,如果第三个参数attachToRoot为true,就将这个root作为根对象返回,否则仅仅将这个root对象的 LayoutParams属性附加到resource对象的根布局对象上,也就是将布局文件resource的布局参数转换为外层root可以接受的类 型,比如root是一个LinearLayout自己要转换的resource里面有layout_width=”fill_parent”,和 layout_height=”fill_parent”参数,但是这些参数没有外部环境,它们对应的对象都是 ViewGroup.LayoutParams对象,root参数让系统将ViewGroup.LayoutParams对象转换为 LinearLayout.LayoutParams对象。
attachToRoot:是否将root附加到布局文件的根视图上
用两个参数的,attachToRoot实际为true返回的是一个跟对象,用false的,可以添加到Activity上
Activity动态添加Fragment时遇到的问题的更多相关文章
- 关于FragmentManager动态管理Fragment时Fragment生命周期的探究
Fragment是Android中的重要组件,在Android 3.0的时候添加进来. 关于Fragment的生命周期,我相信了解过的开发人员都应该把以下方法脱口而出:onAttach, onCrea ...
- Android Fragment用法详解(2)--动态添加Fragment
在上一篇文章<Android Fragment用法详解(1)--静态使用Fragment>我们讲解了Fragment的最简单的用法.这次我们来说一说Fragment复杂一丢丢的用法.在代码 ...
- 使用Fragment 实现动态UI 和 动态添加Fragment
首先写好每个Fragment: 1.在第一个Fragment写一个按钮,使其加载下一个Fragment 布局: <LinearLayout xmlns:android="http:// ...
- 动态添加HTML时onclick函数参数传递
onclick函数动态传参 1.参数为数值类型时: var tmp = 123; var strHTML = "<div onclick=func(" + tmp + &qu ...
- 动态添加Fragment
在Fragment简单用法的基础上做修改 一.新建:another_right_fragment.xml <LinearLayout xmlns:android="http://sch ...
- 动态添加DOM时,绑定的click事件会重复执行
最近因为业务需求,需要重写window的alert和confirm弹窗,但是每次显示的提示按钮不相同,所有每次打开的弹窗都需要重写生成,但是对于相同的按钮会保留上次创建时的click事件,所以当你创建 ...
- Android动态添加Fragment
Android动态添加Fragment 效果图如下: 项目结构图如下: Fragment1: package com.demo.dongtaifragment; import android.app. ...
- angularjs动态添加节点时,绑定到$scope中
<html> <head> <meta charset="utf-8"/> <script src="https://cdn.b ...
- 安卓开发_浅谈Fragment之事务添加Fragment对象
我们都知道给一个activity动态添加fragment的时候 有下面几种添加方式 看一下布局文件 <LinearLayout xmlns:android="http://schema ...
随机推荐
- (8) tomcat中管理领域、角色及用户
srverlet Container或web应用程序本身都可以提供控制web应用程序资源的安全防护 前者称为容器管理的安全防护,或者称为应用程序管理安全防护 通过内嵌机制,tomcat提供一些安全防护 ...
- LEFT JOIN结果集可能变大。。。。。
SELECT A.*,B.* FROM A LEFT JOIN B ON A.ID = B.ID
- python 03 8/25-8/27 range 、randint
import random """字符串的操作中 三种方法,只包含左索引,不包含右索引""" hi= "bokeyuan pyth ...
- 解决sqlplus无法退格问题
# wget http://download.openpkg.org/components/cache/rlwrap/rlwrap-0.42.tar.gz # tar -zxf rlwrap-0. ...
- HTML5表单新增元素与属性
form属性 在html4中,表单的从属元素必须写在表单内部,而在HTML5中,可以把他们书写在任何地方,然后为该元素指定一个form属性,属性值为该表单的id,这样就可以声明该元素从属于指定表单了. ...
- python 连接docker
发现15年写过的一小段,先mark一下,后续玩儿起来 import docker c = docker.Client(base_url='unix://var/run/docker.sock',ver ...
- Vijos1144 皇宫看守 (0/1/2三种状态的普通树形Dp)
题意: 给出一个树以及一些覆盖每个点的花费,求每个点都能被自己被覆盖,或者相邻的点被覆盖的最小价值. 细节: 其实我乍一眼看过去还以为是 战略游戏 的复制版 可爱的战略游戏在这里QAQ(请原谅这波广告 ...
- C++实现链队类——合肥工业大学数据结构实验5:链式队列
实验5 5.1 实验目的 熟练掌握队列的顺序链式存储结构. 熟练掌握队列的有关算法设计,并在链队列上实现. 根据具体给定的需求,合理设计并实现相关结构和算法. 5.2 实验要求 5.2.1链队列实验要 ...
- 国内UED收录
腾讯 腾讯CDC http://cdc.tencent.com/ CDC(Customer Research & User Experience Design Center)腾讯用户研究与体验 ...
- xtu字符串 A. Babelfish
A. Babelfish Time Limit: 3000ms Memory Limit: 65536KB 64-bit integer IO format: %lld Java class ...