@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

Fragment详解之三——管理Fragment(1)

http://blog.csdn.net/harvic880925/article/details/44927375

Fragment使用findFragmentById返回null的更多相关文章

  1. Android Fragment getActivity返回null解决

    在Android开发中,如果我们用到V4包里面的Fragment,在应用被切换到后台的时候,Activity可能被回收,但是创建的所有Fragment则会被保存到Bundle里面,下面是Fragmen ...

  2. 关于FragmentManager findFragmentById 返回nul

    先看Fragment的两种生成方式 一.用xml标签生成 在fragment的宿主activity中添加xml标签 <fragment android:id="@+id/fragmen ...

  3. Android fragment-findFragmentByTag 始终返回 null

    我曾四处看看,在我的案子中找到几个与类似的主题,但没有帮助的问题.我想访问现有活动片段使用getSupportFragmentManager().findFragmentByTag(TAG),但它始终 ...

  4. Android开发:getViewById返回null的原因定位

    近期在研究开发一些基于Android的App,遇到了一些问题.当中一个比較关键的是在Activity中的onCreate()方法中获取Button对象.代码大概例如以下: private Button ...

  5. json_decode返回NULL

    最近在调用某公司的API时,将对方返回的数据,使用PHP的json_decode函数解析,但是返回NULL,最终排查为对方传送来的json格式有误 打印$_REQUEST,数据结构大致如下: arra ...

  6. $.parseJson 在 firefox 下返回 null 的问题

    最近调查一个浏览器兼容性问题,在 IE, chrome下都运行正常,但是在 firefox 下运行时: $.parseJson(xxx) 返回 null,所以导致了 无法正常运行,调查的结果是因为 返 ...

  7. Type.GetType()反射另外项目中的类时返回null的解决方法

    项目1:ProjectA namespace ProjectA { public class paa { .... } } Type.GetType("paa")返回null Ty ...

  8. findViewById返回null

    Q:findViewById返回null? A: 代码逻辑错误: 最终,发现错误竟然是在layout文件中把android:id写成了android:name. android:name=" ...

  9. Xamarin +vs2015 Android 开发GPS loaction 返回 null 小结

    最近公司要开发android 所以研究了一下Xamarin  to android 中个GPS 废话不多说,说重点. 想获取手机上的gps信息必不可少的就是要使用 LocationManager Lo ...

随机推荐

  1. 如何让IE8的菜单栏调到最上方

    如何让IE8的菜单栏调到最上方 运行gpedit.msc,在"用户配置"/"管理模板"/"Windows Components"/" ...

  2. FROM_UNIXTIME()和UNIX_TIMESTAMP()函数的区别

    Unix时间戳(Unix timestamp),是一种时间表示方式,定义为从格林威治时间1970年01月01日00时00分00秒起至现在的总秒数. 在MySQL中如何格式化时间戳?在mysql中因为t ...

  3. banner秒杀

    永远显示 未开始/进行中(需要用到两个for循环,第一个我没有想到,诗詹帮我写的) function timeList(){ myTime = new Date().getTime() var ite ...

  4. python窗体——pyqt初体验

    连续两周留作业要写ftp的作业,从第一周就想实现一个窗体版本的,但是时间实在太短,qt零基础选手表示压力很大,幸好又延长了一周时间,所以也就有了今天这篇文章...只是为了介绍一些速成的方法,还有初学者 ...

  5. Python学习笔记只列表和元组

    一.成员资格 1.检查某个字符是否在某个字符串中:'a' in 'absent' 2.获取数组的长度.最大.最小值: numbers=[100,54,345] len(numbers) //长度 ma ...

  6. 解决Selenium与firefox浏览器版本不兼容问题

    因为在用java打开firefox浏览器的时候报错 org.openqa.selenium.firefox.NotConnectedException: Unable to connect to ho ...

  7. dedecms最新版本修改任意管理员漏洞+getshell+exp

    此漏洞无视gpc转义,过80sec注入防御. 补充下,不用担心后台找不到.这只是一个demo,都能修改任意数据库了,还怕拿不到SHELL? 起因是全局变量$GLOBALS可以被任意修改,随便看了下,漏 ...

  8. 忘记了MariaDB root密码的解决办法

    1.停掉mariaDB systemctl stop mariadb.service 2.KILL掉系统里的MySQL进程: ps -ef | grep mariadb #查询进程PIDkill 进程 ...

  9. 51nod 1134 最长递增子序列

    题目链接:51nod 1134 最长递增子序列 #include<cstdio> #include<cstring> #include<algorithm> usi ...

  10. ios打包

    ios7.1及以上 itms-services://?spm=0.0.0.0.WIsvD2&action=download-manifest&url=https://mtl.aliba ...