使用replace()方法执行FragmentTransaction后,findFragmentByTag()返回null

2024-03-31

我的 Android 应用程序包含三个片段:A、B 和 C。它们被加载到定义的两个容器中MainActivity布局。

当应用程序启动时,它会显示fragmentA 加载到 left_container 中right_container中的fragmentC.

如果您按下片段A, a FragmentTransaction变化片段C by 片段B.

目前一切正常。但是当我尝试使用以下命令获取对加载的fragmentB的引用时,问题就出现了 findFragmentByTag(),因为它返回null。我已经使用了方法替换FragmentTransaction我已经完成了commit(),但是没有办法调用片段B方法。我的代码:

MainActivity.java:

 public class MainActivity extends Activity{
 static String fragmentTag = "FRAGMENTB_TAG";

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    //Adds the left container's fragment
    getFragmentManager().beginTransaction().add(R.id.left_container, new FragmentA()).commit(); //Adds the fragment A to the left container

    //Adds the right container's fragment
    getFragmentManager().beginTransaction().add(R.id.right_container, new FragmentC()).commit(); //Adds the Fragment C to the right container
 }

 /**
 * Called when the button "Activate Fragment B" is pressed
 */
public void buttonListener(View v){

        FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.replace(R.id.right_container, new FragmentB(),fragmentTag); //Replaces the Fragment C previously in the right_container with a new Fragment B
        ft.commit(); //Finishes the transaction


        //!!HERE THE APP CRASHES (java.lang.NullPointerException = findFragmentByTag returns null
        ((FragmentB) getFragmentManager().findFragmentByTag(fragmentTag)).testView();


    }
}

片段B.java:

public class FragmentB extends Fragment {

public View onCreateView(LayoutInflater inflater,
        ViewGroup container,
        Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_b, container,false);

}


/**
 * Gets a reference to the text_fragment_b TextView and calls its method setText(), changing "It doesn't work" text by "It works!"
 */
public void testView(){
    TextView tv = (TextView)getView().findViewById(R.id.text_fragment_b);
    tv.setText("It works!");
}

}

活动_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<FrameLayout android:id="@+id/left_container" android:layout_width="0px" android:layout_weight="50" android:layout_height="match_parent"/>    
<FrameLayout android:id="@+id/right_container" android:layout_width="0px" android:layout_weight="50" android:layout_height="match_parent"/>

</LinearLayout>

片段_b.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" 
android:layout_margin="5sp">

<TextView 
    android:id="@+id/text_fragment_b"
    android:text="It doesn't works!"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

</LinearLayout>

请帮我!我是Android开发的初学者!


我已经修好了!我打了电话getSupportFragmentManager().executePendingTransactions()完成交易后,它成功了!调用该方法后,我可以使用两者来获取片段findFragmentById() and findFragmentByTag()方法。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用replace()方法执行FragmentTransaction后,findFragmentByTag()返回null 的相关文章

  • Android 卷页动画

    我对 Android 动画有点陌生 目前我正在开发一个故事活动 需要像 iPhone 中那样使用卷页动画 我发现 iPhone 中有一种方法可以做到这一点 但我仍然找不到在android中做的方法 所以请帮我解决这个问题 谢谢大家 谷歌代码
  • 如何使用 WifiEnterpriseConfig 设置“使用系统证书”

    我想为我的应用程序的用户配置企业 WiFi 网络 因此 用户必须输入用户名和密码 其余的由应用程序处理 身份验证由 RADIUS 服务器 FreeRadius 3 处理 该服务器使用 LetsEncrypt 颁发的证书 因此无需导入我自己的
  • app-release-unsigned.apk 未签名

    我在 github 上下载了 Android 应用程序的 zip 文件 并尝试运行它 但出现一个包含此消息的对话框 app release unsigned apk is not signed Please configure the si
  • 如何在 Picasso 中使用磁盘缓存?

    我正在使用 Picasso 在我的 Android 应用程序中显示图像 load image This is within a activity so this context is activity public void loadIma
  • Firebase Messaging FCM 在可配置的时间间隔内分发

    当您使用 FCM 向给定应用程序的所有设备发送推送时 这可能会导致许多用户同时打开他们的应用程序 从而导致大量服务器轮询 从而导致负载峰值 有没有一种方便的方法可以在给定的时间间隔内分发消息以进行计划推送 最后 我们找到了一种可能的方法 通
  • 无法在 Android 上编译 avahi

    我是交叉编译的新手 我被分配了使用android补丁的任务http avahi org ticket 354 http avahi org ticket 354将 avahi 核心编译为 android ndk build avahi co
  • 如何在 Android Studio 中构建项目测试目录?

    目前还没有这方面的约定 但是现在我如何构建 Android Studio 的测试目录呢 Android 测试基础 http developer android com tools testing testing android html页面
  • Android WebView文件上传

    我正在开发一个 Android 应用程序 基本上它是一个WebView和一个进度条 Facebook 的移动网站 m facebook com 已加载到WebView 当我单击 选择文件 按钮上传图像时 没有任何反应 我已经尝试了所有的解决
  • 该项目可能使用的 Gradle 版本不包含“compileSdkVersion()”方法

    当我尝试运行最初在 Eclipse ADT 上创建的项目时 我遇到了这个问题 Error 17 0 Gradle DSL method not found compileSdkVersion Possible causes ul li Th
  • ProgressBar.setInminateDrawable() 不起作用

    当我尝试更改我的 indeteminateDrawable 进度条就消失了 我必须更改我的进度条的可绘制对象 我尝试了invalidate requestLayout等 我不知道如何解决它 谢谢 这里的代码 progressBar setI
  • Android volley使用RequestFuture.get()时出现超时异常

    在我的片段中 我尝试使用 TMDB 的开放电影数据库来获取有关 正在播放 电影的详细信息 如果我使用 RequestFuture get time TimeUnit 方法来执行此齐射请求 我总是会收到超时错误 如果我在 Safari 中手动
  • Android:如果任务管理器终止,则重新调用应用程序

    如果应用程序线程被任务管理器杀死 则应用程序线程将关闭 需要重新调用应用程序 就像它被其他应用程序或任务管理器杀死一样 任何想法 您必须使用 START STICKY 命令运行后台服务 只需扩展 Service 并重写 onCommand
  • 从 Android 访问云存储

    我一直无法找到任何有关如何从 Android 应用程序使用云存储的具体文档 我确实遇到过这个客户端库 https cloud google com storage docs reference libraries然而 Google Clou
  • Android 解析 JSON 卡在 get 任务上

    我正在尝试解析一些 JSON 数据 我的代码工作了一段时间 我不确定我改变了什么突然破坏了代码 当我运行代码时 我没有收到任何运行时错误或警告 我创建一个新的 AsyncTask 并执行它 当我打电话时 get 在这个新任务中 调试器在此行
  • 调整 SwipeRefreshLayout 高度,将 View 置于其底部

    I have SwipeRefreshLayout里面一个RelativeLayout 问题是SwipeRefreshLayout占据了屏幕上的所有位置 我需要放置一个视图after这个观点 看图片 https i stack imgur
  • 运行 Android 应用程序时出现错误

    我已经使用 Eclipse 创建了一个 Android 应用程序 但应用程序未在 AVD 上运行 它显示 不幸的是已停止工作 日志猫消息如下 07 29 04 59 50 789 W dalvikvm 784 threadid 1 thre
  • 将触摸事件从 NestedScrollView 传递到父视图

    我在 NestedScrollView 下方有一个 ViewPager 宽度一些顶部填充 以及 ClipToPadding false 和透明背景 如图像 我的 ViewPager 无法获取触摸事件并且无法工作 我怎么解决这个问题 我无法更
  • Android OptionsMenu问题,背景始终透明

    我的选项菜单总是不显示背景 背景是透明的 有谁知道如何摆脱这个 我的失败起源活动是从另一个自定义活动扩展的 我在 eclipse 上有这个项目 选项菜单工作正常 但自从我迁移到 AndroidStudio 后 选项菜单始终是透明的 我尝试更
  • 在循环中按名称访问变量

    我正在开发一个 Android 项目 并且有很多可绘制对象 这些绘图的名称都类似于icon 0 png icon 1 png icon 100 png 我想将这些可绘制对象的所有资源 ID 添加到整数 ArrayList 中 对于那些不了解
  • 如果我的应用程序安装在 SD 卡上,私人数据也在那里吗?

    我假设应用程序的私有数据 例如 SharedPreferences 和 SQLite 数据库 位于手机的内部存储而不是 SD 卡上 即使应用程序本身安装在 SD 卡上 我在任何地方都找不到对此的简单明确的确认 有人可以确认一下吗 是的 私有

随机推荐