Android Fragment 嵌套使用报错
在新的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 嵌套使用报错的更多相关文章
- Android中editText使用报错
在activity_main.xml文件中添加了editText控件 <EditText android:id="@+id/edit_text" ...
- adb驱动安装和使用报错笔记
adb驱动安装 adb驱动下载地址:https://adb.clockworkmod.com/ 安装时候选择一个容易记住的路径,这个很重要,因为adb驱动没有自动配置环境变量,所以实验时候将adb安装 ...
- animate is not a function(zepto 使用报错)[转]
animate is not a function(zepto 使用报错) 1.为什么使用zepto写animate报错? 因为zepto默认构建包含: Core, Ajax, Event, Form ...
- 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 ...
- yum源使用报错
CentOS系统yum源使用报错:Error: Cannot retrieve repository metadata (repomd.xml) for repository: rpmforge. 服 ...
- 2019-9-9:渗透测试,docker下载dvwa,使用报错型sql注入dvwa
docker下载dvwa镜像,报错型注入dvwa,low级 一,安装并配置docker 1,更新源,apt-get update && apt-get upgrade &&am ...
- .net core中Grpc使用报错:The remote certificate is invalid according to the validation procedure.
因为Grpc采用HTTP/2作为通信协议,默认采用LTS/SSL加密方式传输,比如使用.net core启动一个服务端(被调用方)时: public static IHostBuilder Creat ...
- VirtualBox使用报错
VirtualBox使用报错 1.启动报错:Failed to instantiate CLSID_VirtualBox... 报错内容: Failed to instantiate CLSID_Vi ...
- 浅谈Android Fragment嵌套使用存在的一些BUG以及解决方法
时间 2014-03-18 18:00:55 eoe博客 原文 http://my.eoe.cn/916054/archive/24053.html 主题 安卓开发 自从Android3.0引入了F ...
随机推荐
- wget www.baidu.com执行流程分析
下载网页的关键函数: main.c(main) b 1394 url_parse:解析url,获取url相关信息,返回结构体 struct url 的指针,存于 url_parsed retrieve ...
- 概率法求解三阶幻方[C语言]
#include <stdio.h> #include <string.h> ]={,,,,,,,,}; ]; ][]; int sum(int su[]) { ; ;su[i ...
- 转 批处理 %~dp0的意义
http://nealcai.iteye.com/blog/1685192 http://blog.csdn.net/caz28/article/details/7448677 http://stac ...
- Krita编译和旧版本下载
Linux For Krita 2.9, David Revoy's guide Building Krita for Cats is the best available! Build Krita ...
- hdu 2850 Load Balancing (优先队列 + 贪心)
题目大意: 怎么分配n个任务到m个server上使得负载尽量平衡. 思路: 将任务从大到小排序,依次放入负载最小的那个server中. 由于是spj 的缘故,所以能够使用这个贪心. 比方数据 6 2 ...
- Deep Learning(深度学习)学习笔记整理系列之(七)
Deep Learning(深度学习)学习笔记整理系列 zouxy09@qq.com http://blog.csdn.net/zouxy09 作者:Zouxy version 1.0 2013-04 ...
- WPF属性与特性的映射(TypeConverter)
1,定义一个类 public class Human { public string Name { get; set; } public Human Child { get; set; } } 2在X ...
- 更新cydia“sub-process/usr/libexec/cydia/cydo returned anerror code(2)”是怎么回事?
最近更新cydia的时候出现了sub-process/usr/libexec/cydia/cydo returned anerror code(2)的红字是怎么回事? 解决方法:删掉有关升级的东西,把 ...
- 原生js判断某个元素是否有指定的class名的几种方法
[注意]以下方法只对class只有一个值的情况下操作 ************************************************************* 结构部分: <d ...
- springmvc+mybatis集成配置
简单之美,springmvc,mybatis就是一个很好的简单集成方案,能够满足一般的项目需求.闲暇时间把项目配置文件共享出来,供大家参看: 1.首先我们来看下依赖的pom: <!-- spri ...