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时遇到的问题的更多相关文章

  1. 关于FragmentManager动态管理Fragment时Fragment生命周期的探究

    Fragment是Android中的重要组件,在Android 3.0的时候添加进来. 关于Fragment的生命周期,我相信了解过的开发人员都应该把以下方法脱口而出:onAttach, onCrea ...

  2. Android Fragment用法详解(2)--动态添加Fragment

    在上一篇文章<Android Fragment用法详解(1)--静态使用Fragment>我们讲解了Fragment的最简单的用法.这次我们来说一说Fragment复杂一丢丢的用法.在代码 ...

  3. 使用Fragment 实现动态UI 和 动态添加Fragment

    首先写好每个Fragment: 1.在第一个Fragment写一个按钮,使其加载下一个Fragment 布局: <LinearLayout xmlns:android="http:// ...

  4. 动态添加HTML时onclick函数参数传递

    onclick函数动态传参 1.参数为数值类型时: var tmp = 123; var strHTML = "<div onclick=func(" + tmp + &qu ...

  5. 动态添加Fragment

    在Fragment简单用法的基础上做修改 一.新建:another_right_fragment.xml <LinearLayout xmlns:android="http://sch ...

  6. 动态添加DOM时,绑定的click事件会重复执行

    最近因为业务需求,需要重写window的alert和confirm弹窗,但是每次显示的提示按钮不相同,所有每次打开的弹窗都需要重写生成,但是对于相同的按钮会保留上次创建时的click事件,所以当你创建 ...

  7. Android动态添加Fragment

    Android动态添加Fragment 效果图如下: 项目结构图如下: Fragment1: package com.demo.dongtaifragment; import android.app. ...

  8. angularjs动态添加节点时,绑定到$scope中

    <html> <head> <meta charset="utf-8"/> <script src="https://cdn.b ...

  9. 安卓开发_浅谈Fragment之事务添加Fragment对象

    我们都知道给一个activity动态添加fragment的时候 有下面几种添加方式 看一下布局文件 <LinearLayout xmlns:android="http://schema ...

随机推荐

  1. Linux vsftpd服务

    vsftpd服务 由vsftpd包提供 不再由xinetd管理 用户认证配置文件:/etc/pam.d/vsftpd 服务脚本: /usr/lib/systemd/system/vsftpd.serv ...

  2. 第一讲:vcs simulation basic

    要求: 1.complie a verilog/systemverilog design using vcs 2.simulate a verilog/systemverilog design vcs ...

  3. 第七章:systemverilog过程语句

    systemverilog增加了一些新的操作符和过程语句: 1.新的操作符 递增/递减 赋值操作符 设置成员操作符inside 有无关通配符==?/!=? 操作数改进(类型/尺寸/符号强制转换) 2. ...

  4. LeetCode(66)Plus One

    题目 Given a non-negative number represented as an array of digits, plus one to the number. The digits ...

  5. 3D地形中的道路模拟

    笔者注: 这篇文章是我本人在2009年发表在cppblog的一篇技术文章,由于我的技术博客迁移至博客园,所以转载到了此,非盗文. 以下是正文: 前段时间被项目组长委派实现基于3D地形的道路系统.实现的 ...

  6. bootspring + mybaits +mysql Date 类型的处理

    mysql 中有date 类型的属性,java实体类中对应的属性是java.sql.Date 类的. 最初的bug是怎么新增,joinDate 值都是null. 千辛万苦学会了用String转Date ...

  7. 大数据学习——有两个海量日志文件存储在hdfs

    有两个海量日志文件存储在hdfs上, 其中登陆日志格式:user,ip,time,oper(枚举值:1为上线,2为下线):访问之日格式为:ip,time,url,假设登陆日志中上下线信息完整,切同一上 ...

  8. 数据库服务器的监控 赛门铁克 Veritas i3 APM 查找指定时间段最耗服务器资源的TopSQL

  9. Leetcode 227.基本计算器II

    基本计算器II 实现一个基本的计算器来计算一个简单的字符串表达式的值. 字符串表达式仅包含非负整数,+, - ,*,/ 四种运算符和空格  . 整数除法仅保留整数部分. 示例 1: 输入: " ...

  10. Linux(2):基础命令

    linux 的规则: 1. linux 命令行组成结构:如下 [root@neo ~]# [用户名@主机名 当前工作路径]# ~ 用户的家目录 2. linux系统命令操作语法的格式(命令的样子): ...