在新的SDK每次创建activity时,会自己主动生成 

<pre name="code" class="java">public static class PlaceholderFragment extends Fragment

fragment模块,在该模块的基础上进行嵌套fragment代码例如以下:

<pre name="code" class="java">public static class PlaceholderFragment extends Fragment {

		static FragmentManager fm;
public PlaceholderFragment() {
fm=getChildFragmentManager();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); } @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView=LayoutInflater.from(getActivity())
.inflate(R.layout.tab, null); if(fm.findFragmentByTag("haha")==null)
{
Fragment1 f1=new Fragment1();
fm.beginTransaction().add(R.id.frame_tab,f1,"haha").commit();
}
return rootView;
}


结果报错例如以下:

总之就是说Acitivity被被销毁了。

原因:

fm=getChildFragmentManager();在 <span style="font-family: Arial, Helvetica, sans-serif;">PlaceholderFragment 的构造函数中调用,此时它还没有创建onCreate,自然获取不到Fragment及activity了,因此将其写入</span>
<span style="font-family: Arial, Helvetica, sans-serif;"></span><pre name="code" class="java"><pre name="code" class="java">public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
fm=getChildFragmentManager();
}

错误解决,大家一定要对Fragment及activity生命周期注意啊。



Android Fragment 嵌套使用报错的更多相关文章

  1. Android中editText使用报错

    在activity_main.xml文件中添加了editText控件 <EditText        android:id="@+id/edit_text"        ...

  2. adb驱动安装和使用报错笔记

    adb驱动安装 adb驱动下载地址:https://adb.clockworkmod.com/ 安装时候选择一个容易记住的路径,这个很重要,因为adb驱动没有自动配置环境变量,所以实验时候将adb安装 ...

  3. animate is not a function(zepto 使用报错)[转]

    animate is not a function(zepto 使用报错) 1.为什么使用zepto写animate报错? 因为zepto默认构建包含: Core, Ajax, Event, Form ...

  4. Windows下Git使用报错:warning:LF will be replaced by CRLF in ××××.××

    Windows下Git使用报错: warning:LF will be replaced by CRLF in ××××.××(文件名) The file will have its original ...

  5. yum源使用报错

    CentOS系统yum源使用报错:Error: Cannot retrieve repository metadata (repomd.xml) for repository: rpmforge. 服 ...

  6. 2019-9-9:渗透测试,docker下载dvwa,使用报错型sql注入dvwa

    docker下载dvwa镜像,报错型注入dvwa,low级 一,安装并配置docker 1,更新源,apt-get update && apt-get upgrade &&am ...

  7. .net core中Grpc使用报错:The remote certificate is invalid according to the validation procedure.

    因为Grpc采用HTTP/2作为通信协议,默认采用LTS/SSL加密方式传输,比如使用.net core启动一个服务端(被调用方)时: public static IHostBuilder Creat ...

  8. VirtualBox使用报错

    VirtualBox使用报错 1.启动报错:Failed to instantiate CLSID_VirtualBox... 报错内容: Failed to instantiate CLSID_Vi ...

  9. 浅谈Android Fragment嵌套使用存在的一些BUG以及解决方法

    时间 2014-03-18 18:00:55 eoe博客 原文  http://my.eoe.cn/916054/archive/24053.html 主题 安卓开发 自从Android3.0引入了F ...

随机推荐

  1. SQL Server 一些重要视图4

    sys.master_files 为每一个数据库的每一个文件返回行.

  2. XP下Virtualbox虚拟Ubuntu共享文件夹设置

    环境:主机操作系统是Windows XP,虚拟机是Ubuntu 8.10,虚拟机是VirtualBox 2.1.0. 1. 安装增强功能包(Guest Additions) 安装好Ubuntu 8.1 ...

  3. rsyslog 日志格式和输出

    日志格式: $EscapeControlCharactersOnReceive off #关闭rsyslog默认转译ASCII<32的所有怪异字符,包括换行符等 $template nginx- ...

  4. 十几个remote control software

    5 alternatives to LogMeIn Free for remote PC access VNC VNC, or Virtual Network Computing, isn’t its ...

  5. javascript数组顺序-----1冒泡的另一种比较好理解的写法

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. 字典 -- 数据结构与算法的javascript描述 第七章

    字典 字典是一种以键-值对形式存储数据的数据结构 最基本功能规划 add 添加数据到字典 remove 从字典中移除数据 get 从字典中取出数据 count 统计字典数据量 find 查找数据在字典 ...

  7. 关于ECMAScript6一些知识

    ECMAScript 是当前 JavaScript 语言规范的最新标准,一般称为 es6, 但是因为 该标准规范是在 2015年6月份发布的,所以也叫作 ECMAScript 2015 let 变量声 ...

  8. 8月9日,PS、计算机基础(预科)

    一.   PS         掌握简单的图标修改. 1.图层                 2.保存PSD格式,有图层:JPG格式,没有图层.                 3.魔棒工具(调整值 ...

  9. CXF 调用C#.net的WebService

    原文链接:http://hi.baidu.com/pengfeiiw/blog/item/3203e29065aa3a8aa977a4d0.html 1.编写C#.net的WebService Ser ...

  10. hosts文件简析

    什么是hosts文件 hosts文件是个什么文件呢?Hosts虽然没有后缀名,其实是个纯文本文件,可以用记事本等文本编辑软件打开.Hosts文件主要用于在本地电脑强制解析域名,Hosts文件里包含映射 ...