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 ...
随机推荐
- 使用finalshll连接linux
一.安装ubuntu: 我在window10上装了VMware,好像window10自带虚拟机吧;然后傻瓜式装机,装好后发现没网不知道什么原因,然后百度啪啦啪啦找了一堆,解决方法是: 然后重启下ubu ...
- Python数据可视化库-Matplotlib(二)
我们接着上次的继续讲解,先讲一个概念,叫子图的概念. 我们先看一下这段代码 import matplotlib.pyplot as plt fig = plt.figure() ax1 = fig.a ...
- 【瞎扯】我的OI之路
这里大概是一些我自己对我的OI之路的一些记录. 2015.11不知道哪一天-- 我听说了"编程". 当时还不懂得啥是信息学竞赛,以为这只是纯粹的程序设计.后来才明白信息学竞赛是算法 ...
- ajax跨域访问总结
1,jsonp的使用 就是script引用别的站点js,利用回调把内容传给这个js。 a需要引入b,在页面上引入b的js,里面有b的函数,在a中执行,就能拿到json了。 程序B中test.js的代码 ...
- STM32F407 串口通信实验 视频第27节 个人笔记
前言 第26节也是串口,笔记链接在此:https://www.cnblogs.com/YuQiao0303/p/10019362.html github地址:https://github.com/Yu ...
- Java比较两个数组中的元素是否相同的最简单方法
import java.util.Arrays; public class Test { /** * Java比较两个数组中的元素是否相同 */ public static void main(Str ...
- UVA 11090 判负圈问题
题目链接http://vjudge.net/problem/viewProblem.action?id=34650 题目大意: 给定n个点m条边的加权有向图,求平均权值最小的回路.平均权值=路径权值之 ...
- mybatis查询返回null解决方案
mybatis查询返回null解决方案: 问题:查询出的列与javabean中的字段名不一致. 解决方案: 1.将javabean中的字段改为和查询出的列名一致: 2.将sql加入as改变列名,和ja ...
- 事件和委托:第 5 页 委托、事件与Observer设计模式
原文发布时间为:2008-11-01 -- 来源于本人的百度文章 [由搬家工具导入] 委托、事件与Observer设计模式 范例说明 上面的例子已不足以再进行下面的讲解了,我们来看一个新的范例,因为之 ...
- linux 安装报错:pkg-config not found
linux 安装报错:pkg-config not found 使用编译安装时,在执行./configure时报如下错误: ... ... checking for pkg-config... no ...