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 ...
随机推荐
- 限定只能处理"A仓"和"B仓"入库
应用 Oracle Inventory 层 Level Function 函数名 Funcgtion Name INV_INVTTMTX_MISC 表单名 Form Name INVTTMTX 说明 ...
- Windows 7妙用 笔记本变无线AP轻松共享
笔记本变AP的前提和应用原理 笔记本变AP的前提是你所处的房间或地点需要提供有线宽带的连接,而且你的笔记本要有无线网卡.如果这两个条件具备了,即使没有路由器/无线AP等辅助设备,多个笔记本电脑共享上网 ...
- 数组Magic Index
Question A magic index in an array A[1...n-1] is defined to be an index such that A[i] = i. Given a ...
- /etc/security/limits.conf 配置
<pre name="code" class="python">* soft nofile 65535 * hard nofile 65535 * ...
- mysql查询语句理解
看一个查询语句 ,)) as passcount FROM (SELECT b.user,b.full_name,b.user_group From login_log a LEFT JOIN vic ...
- [LeetCode][Python]18: 4Sum
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 18: 4Sumhttps://oj.leetcode.com/problem ...
- 编程之美之数独求解器的C++实现方法
编程之美的第一章的第15节.讲的是构造数独.一開始拿到这个问题的确没有思路, 只是看了书中的介绍之后, 发现原来这个的求解思路和N皇后问题是一致的. 可是不知道为啥,反正一開始确实没有想到这个回溯法. ...
- php 设置字符集为utf-8
header("Content-Type:text/html;charset=utf-8");
- 浅谈API设计
为什么需要了解一些API设计? 只要你编程,你就是API Designer 一个好的设计,模块之间的耦合应该也是API级别的 一个程序,如果你独立开发,那你既是API的Designer,也是API的U ...
- iframe使用location跳转页面的问题
iframe页面调用父级页面中的函数 parent.dofunction(); contentWindow 实例 iframe = document.getElementById("fram ...