我曾四处看看,在我的案子中找到几个与类似的主题,但没有帮助的问题。我想访问现有活动片段使用getSupportFragmentManager().findFragmentByTag(TAG),但它始终返回null。有关类似问题的答复建议它需要提交要执行的一段时间,因此调用 findFragmentByTag 将返回 null (如果叫得太早。我试过两件事:

  • 添加getSupportFragmentManager().executePendingTransactions()
    立即后提交,但仍得到null。
  • 添加按钮......这按创建活动后,该片段注册和显示的视图应离开系统足够的时间来提交。但是我还是为 null。

这里是我的活动:

public class MainActivity extends ActionBarActivity {

private static final String F_SETTINGS = "f_settings";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
debug();
}
}); if (savedInstanceState == null) {
FSettings newFragment = new FSettings();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.container, newFragment);
ft.addToBackStack(F_SETTINGS);
ft.commit();
// getSupportFragmentManager().executePendingTransactions();
//// Activating this did not make any difference...
} debug();
} private void debug() {
String txt = "null";
Fragment frag = getSupportFragmentManager().findFragmentByTag(F_SETTINGS);
if (frag != null) {
txt = frag.toString();
}
Log.i("Testing", txt);
}
}

我在做什么在这里错了?欢呼声中,最大

解决方法 1:

在您的代码中你没那么,提及中替换方法的标记
使用这种结构的替换方法的片段

ft.replace(R.id.container, newFragment,"fragment_tag_String");

Android fragment-findFragmentByTag 始终返回 null的更多相关文章

  1. Fragment使用findFragmentById返回null

    @Override public void onClick(View v) { switch (v.getId()){ case R.id.btn1: replaceFragment(new Anot ...

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

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

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

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

  4. 注册表键值明明存在OpenSubKey始终返回null,解决方案

    先上代码及实例 RegistryKey rsg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Macromedia\FlashPaper Pri ...

  5. 混用Int与IntPtr导致GetProcAddress始终返回null

      注意NET某些类型在不同平台上的长度 NET中用句柄用得最多的是在DLLIMPORT中,混用int与intptr可能会导致某些API声明在X64平台中表现不正常,如 [DllImport(&quo ...

  6. Android 成功 使用GPS获取当前地理位置(解决getLastKnownLocation 返回 null)

    最近遇到一个比较棘手的问题:使用GPS定位无法获取当前的地理位置,即getLastKnownLocation方法始终返回null.   后来一篇博文 getLastKnownLocation()返回n ...

  7. Android Fragment getActivity返回null解决

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

  8. Attempt to write to field 'android.support.v4.app.FragmentManagerImpl android.support.v4.app.Fragment.mFragmentManager' on a null object reference

    E/AndroidRuntime﹕ FATAL EXCEPTION: mainProcess: org.example.magnusluca.drawertestapp, PID: 3624java. ...

  9. Android GsmCellLocation.getCellLocation返回NULL

    Android GsmCellLocation.getCellLocation返回NULL 1.首先 获取服务 telephonyManager =(TelephonyManager)getSyste ...

随机推荐

  1. [luoguP2904] [USACO08MAR]跨河River Crossing(DP)

    传送门 f[i] 表示送前 i 头牛过去再回来的最短时间 f[i] = min(f[i], f[j] + sum[i - j] + m) (0 <= j < i) ——代码 #includ ...

  2. SCOI2010第一场

    NOI2010全国青少年信息学奥林匹克竞赛 四川代表队选拔赛 第一场 题目名称 幸运数字 游戏 股票交易 英文代号 luckynumber game trade 时限 2秒 2秒 2秒 输入文件 lu ...

  3. Stealing Harry Potter's Precious BFS+DFS

    Problem Description Harry Potter has some precious. For example, his invisible robe, his wand and hi ...

  4. MyBatis 3判断不为null

    <if test="type!=null and type!=''"> AND type = #{type} </if>

  5. 【C语言】模拟实现strcmp函数

    //模拟实现strcmp函数 //str1>str2,返回1 //str1=str2,返回0 //str1<str2,返回-1 #include <stdio.h> #incl ...

  6. 开源软件Review Board

    开源软件, Review Board 代码审查的. https://www.reviewboard.org/

  7. HTML5:表格

    表格的作用是显示二维数据.在HTML5中不再同意用表格控制页面内容的布局.而是採用新增的CSS表格特性(这里不涉及CSS,将在后面介绍). 以下主要介绍用于制作表格的HTML元素. 构建表格 表格的基 ...

  8. Linux声卡驱动移植和測试

    一.分析驱动程序,依据开发板改动代码 代码太长,就不贴了,几个注意点: 1. 查看开发板原理图和S3C2410的datasheet,UDA1341的L3MODE.L3DATA.L3CLOCK分别与S3 ...

  9. Erlang下与其他程序和语言的通信机制(2)

    前面聊了普通端口,今天聊下链入式驱动端口,以及NIFs. 链入式驱动端口 如上图所示,链入式驱动端口与Erlang虚拟机存在于同一个OS进程中. 在Erlang这边与普通端口类似,所有与链入式驱动端口 ...

  10. 一个样例看清楚JQuery子元素选择器children()和find()的差别

    近期在我们的hybrid app项目开发中定位出了一个问题.通过这个问题了解下JQuery选择器find()和children()的差别.问题是这种:我们的混合app是一个单页面应用(main.htm ...