Fragment使用findFragmentById返回null
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn1:
replaceFragment(new AnotherRightFragment(),"anotherRightFragment");
// //在activity中获得fragment实例
// AnotherRightFragment ar = (AnotherRightFragment)getFragmentManager().findFragmentByTag("anotherRightFragment");
// Log.d(TAG, "onClick: "+ar);
// ar.setText("文字哈");
break;
case R.id.btn2:
AnotherRightFragment ar = (AnotherRightFragment)getFragmentManager().findFragmentByTag("anotherRightFragment");
Log.d(TAG, "onClick: "+ar);
ar.setText("文字哈");
break;
default:
break;
}
}
/**
*动态添加fragment
*/
private void addFragment(Fragment fragment){
FragmentManager fm = getFragmentManager();
fm.beginTransaction().add(R.id.right_layout,fragment).commit();
// transaction.addToBackStack(null);
}
/**
*动态添加fragment
*/
private void replaceFragment(Fragment fragment,String tag){
FragmentManager fm = getFragmentManager();
fm.beginTransaction().replace(R.id.right_layout,fragment,tag).commit();
}
在替换之后马上
AnotherRightFragment ar = (AnotherRightFragment)getFragmentManager().findFragmentByTag("anotherRightFragment");
会获取不到这个fragment。
findFragmentById是获取 xml布局文件显示<fragment android:name="+@id/fid"/> 声明的。
findFragmentByTag能动态获取
查了下fragment的生命周期, 添加一个碎片->onAttach()->onCreate()->onCreateView()
/**
*动态添加fragment
*/
private void replaceFragment(Fragment fragment,String tag){
FragmentManager fm = getFragmentManager();
FragmentTransaction tc = fm.beginTransaction();
tc.replace(R.id.right_layout,fragment,tag);
tc.addToBackStack(null);
tc.commit();
//立即执行
fm.executePendingTransactions();
}
add或者replace只是添加到任务,如果立即执行得加上executePendingTransactions。这样才能紧跟着通过fragmentmanager获取fragment
manager.findFragmentById(); //根据ID来找到对应的Fragment实例,主要用在静态添加fragment的布局中,因为静态添加的fragment才会有ID
manager.findFragmentByTag();//根据TAG找到对应的Fragment实例,主要用于在动态添加的fragment中,根据TAG来找到fragment实例
manager.getFragments();//获取所有被ADD进Activity中的Fragment
http://blog.csdn.net/harvic880925/article/details/44927375
Fragment使用findFragmentById返回null的更多相关文章
- Android Fragment getActivity返回null解决
在Android开发中,如果我们用到V4包里面的Fragment,在应用被切换到后台的时候,Activity可能被回收,但是创建的所有Fragment则会被保存到Bundle里面,下面是Fragmen ...
- 关于FragmentManager findFragmentById 返回nul
先看Fragment的两种生成方式 一.用xml标签生成 在fragment的宿主activity中添加xml标签 <fragment android:id="@+id/fragmen ...
- Android fragment-findFragmentByTag 始终返回 null
我曾四处看看,在我的案子中找到几个与类似的主题,但没有帮助的问题.我想访问现有活动片段使用getSupportFragmentManager().findFragmentByTag(TAG),但它始终 ...
- Android开发:getViewById返回null的原因定位
近期在研究开发一些基于Android的App,遇到了一些问题.当中一个比較关键的是在Activity中的onCreate()方法中获取Button对象.代码大概例如以下: private Button ...
- json_decode返回NULL
最近在调用某公司的API时,将对方返回的数据,使用PHP的json_decode函数解析,但是返回NULL,最终排查为对方传送来的json格式有误 打印$_REQUEST,数据结构大致如下: arra ...
- $.parseJson 在 firefox 下返回 null 的问题
最近调查一个浏览器兼容性问题,在 IE, chrome下都运行正常,但是在 firefox 下运行时: $.parseJson(xxx) 返回 null,所以导致了 无法正常运行,调查的结果是因为 返 ...
- Type.GetType()反射另外项目中的类时返回null的解决方法
项目1:ProjectA namespace ProjectA { public class paa { .... } } Type.GetType("paa")返回null Ty ...
- findViewById返回null
Q:findViewById返回null? A: 代码逻辑错误: 最终,发现错误竟然是在layout文件中把android:id写成了android:name. android:name=" ...
- Xamarin +vs2015 Android 开发GPS loaction 返回 null 小结
最近公司要开发android 所以研究了一下Xamarin to android 中个GPS 废话不多说,说重点. 想获取手机上的gps信息必不可少的就是要使用 LocationManager Lo ...
随机推荐
- VMware技巧01
1.20160930 VMware® Workstation 10.0.4 build-2249910,使用中遇到问题(WinXP sp3):网卡 桥接模式,NAT模式 都连不上网... 今天,尝试了 ...
- 解决:“java.lang.IllegalArgumentException: error at ::0 can't find referenced pointcut myMethod”问题!
Spring版本:2.5.6 AspectJ是Spring自带的lib. Jdk版本:1.7.0_17 在配置没问题的情况下,报:java.lang.IllegalArgumentException: ...
- SPSS数据分析—分段回归
在SPSS非线性回归过程中,我们讲到了损失函数按钮可以自定义损失函数,但是还有一个约束按钮没有讲到,该按钮的功能是对自 定义的损失函数的参数设定条件,这些条件通常是由逻辑表达式组成,这就使得损失函数具 ...
- CentOS 下 rpm包与 yum 安装与卸载
rpm包的安装: 1.安装一个包 # rpm -ivh 2.升级一个包 # rpm -Uvh 3.移走一个包 # rpm -e 4.安装参数 --force 即使覆盖属于其它包的文件也强迫安 ...
- loading
<!doctype html> <html> <head> <title>实惠福利</title> <meta charset=&qu ...
- dos2unix,去掉Linux下文件中的^M
Windows系统下使用VS2010编写好的CPP文件,想放到Linux上进行编译.发现Linux上文件中的每行代码末尾都跟着^M这个符号. 为什么同一份文件在windows上和Linux上显示的不一 ...
- iOS开发Swift篇—(七)函数(1)
iOS开发Swift篇—(七)函数 一.函数的定义 (1)函数的定义格式 func 函数名(形参列表) -> 返回值类型 { // 函数体... } (2)形参列表的格式 形参名1: 形参类型1 ...
- 获取LocationProvider
Android的定位信息由LocationProvider对象来提供,该对象代表一个抽象的定位组件.在开始编程之前,需要先获得LocationProvider对象. 一.获取所有可用的Location ...
- 【56测试】【字符串】【dp】【记忆化搜索】【数论】
第一题:神秘大门 大意: 两个字符串A,B,按字典序最大的顺序输出B 的每个字符在A 中的位置,如果B不全在A中,输出No,否则Yes. 解: 这道题就是一遍的扫描,因为要按字典序最大的输出,所以从后 ...
- php函数的可变参数
<?php function add() { $arr = func_get_args(); //func_num_args() $sum =0; for($i=0;$i<count($a ...