在新的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. ListView的简单使用和性能优化

    起源:ListView是Android开发中使用最广泛的一种控件,它以垂直列表的形式显示所有列表项. 创建ListView有两种方式: ☆ 直接使用ListView进行创建. ☆让Activity继承 ...

  2. 致命错误C0000034正在应用更新操作37解决方法

    当碰到这个问题的时候关闭电脑,启动,不断按F8键,进入命令行,输入Notepad.exe弹出记事本,文件,打开,所有文件,然后将C:\Windows\WinSxS目录下的pending.xml文件中的 ...

  3. EPiServer网文

    ListItemCollection Rending: http://joelabrahamsson.com/episerver-7-and-mvc-how-to-customize-renderin ...

  4. 一个大小为N的数组,里面是N个整数,怎样去除重复的数

    题目:一个大小为N的数组,里面是N个整数,怎样去除重复的数字: 要求时间复杂度为O(n),空间复杂度为O(1). 需要除掉重复的整数的数组,注意这里我没有处理负数情况,其实负数情况只要先用0快排分一下 ...

  5. centos6 qt ENV

    打算做嵌入式图像处理,计划方案嵌入式Linux+OpenCV+QT,昨天简单入门OpenCV今天看看QT,QT就先弄Linux下面的,回家之前争取把基本的摸通,然后能在板子上跑起来 关于QT安装 QT ...

  6. 三点顺序_nyoj_68(计算几何).java

    三点顺序 时间限制: 1000 ms  |  内存限制: 65535 KB 难度: 3   描述 现在给你不共线的三个点A,B,C的坐标,它们一定能组成一个三角形,现在让你判断A,B,C是顺时针给出的 ...

  7. 面向对象程序设计-C++_课时14对象组合_课时15继承

    对象组合,就是一个类的对象作为另外一个类的成员,涉及类的对象,对象是实体,玩实 继承,涉及类,类是概念,玩虚 public: 所有人都可以接触 private: 数据放private protecte ...

  8. 菜鸟运维笔记:安装MySQL,PHP及phpMyAdmin

    转载请注明出处:http://blog.csdn.net/guodongxiaren/article/details/40684799 阿里云提示我备案,挺烦人的.前天通过github的学生开发包. ...

  9. VBA 开发学习--基础语法2

    VBA中的运算符 算数运算符及其作用 + 求两个数的和 -  求两个数的差:求一个数的相反数 *  求两个数的积 /   求两个数的商(保留小数位   5/2=2.5) \   整除 (保留整数位  ...

  10. Matlab中K-means聚类算法的使用(K-均值聚类)

    K-means聚类算法采用的是将N*P的矩阵X划分为K个类,使得类内对象之间的距离最大,而类之间的距离最小. 使用方法:Idx=Kmeans(X,K)[Idx,C]=Kmeans(X,K) [Idx, ...