android - getSupportFragmentManager().findFragmentByTag() 返回 null

标签 android android-fragments

我正在尝试通过获取容器中的当前 fragment 并调用其方法来更新我的 UI。我的布局如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<android.support.v4.widget.DrawerLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/drawer_layout"
     android:layout_width="match_parent"
     android:layout_height="match_parent">
     <!-- The main content view -->
     <FrameLayout
         android:id="@+id/frmContentFrame"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:clickable="true" />
     <!-- The navigation drawer -->
     <ListView android:id="@+id/lstLeftDrawer"
         android:layout_width="240dp"
         android:layout_height="match_parent"
         android:layout_gravity="start"
         android:choiceMode="singleChoice"
         android:background="@color/SlideMenuBlue"
         android:divider="@color/White"
         android:dividerHeight="1dp"
         android:paddingTop="@dimen/list_padding"
         android:paddingBottom="@dimen/list_padding"/>
</android.support.v4.widget.DrawerLayout>

我像这样在 Fragments(android.support.v4.app) 之间转换

//Switches views
public void switchContent(final Fragment fragment) {
    getSupportFragmentManager()
    .beginTransaction()
    .replace(R.id.frmContentFrame, fragment, "CURRENT_FRAGMENT")
    .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
    .addToBackStack(null)
    .commitAllowingStateLoss();
}

当我调用更新 UI 方法时,我的 fragment 为空。这是代码

public void updateUI() {
    Fragment fragment = getSupportFragmentManager().findFragmentByTag("CURRENT_FRAGMENT");
    System.out.println(fragment);

    if((FragmentA) getSupportFragmentManager().findFragmentByTag("CURRENT_FRAGMENT") != null){
        FragmentA frag = (FragmentA) getSupportFragmentManager().findFragmentByTag("CURRENT_FRAGMENT");
        frag.updateUIStatusA();
    }
    else if((FragmentB) getSupportFragmentManager().findFragmentByTag("CURRENT_FRAGMENT") != null){
        FragmentB frag = (FragmentB) getSupportFragmentManager().findFragmentByTag("CURRENT_FRAGMENT");
        frag.updateUIStatusB();
    }
}

奇怪的是,当 fragment 返回 null 时,我得到一个 ClassCastException,如下所示

java.lang.ClassCastException: com.example.activity.fragments.FragmentB cannot be cast to com.example.activity.fragments.FragmentA

为什么它在错误中知道我的容器中有什么 fragment ,但当我尝试检索它时却返回 null?

任何帮助都会很棒

谢谢

最佳答案

尝试使用独特的标签,不要忘记在 backstack 中添加您的标签。示例:

//Switches views
public void switchContent(final Fragment fragment) {
   getSupportFragmentManager()
   .beginTransaction()
   .replace(R.id.frmContentFrame, fragment, fragment.getClass().getSimpleName())
   .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
   .addToBackStack(fragment.getClass().getSimpleName())
   .commitAllowingStateLoss();
}

那么你的更新方法可能是这样的

public void updateUI() {
Fragment fragmentA = getSupportFragmentManager().findFragmentByTag(FragmentA.class.getSimpleName());
Fragment fragmentB = getSupportFragmentManager().findFragmentByTag(FragmentB.class.getSimpleName());

if (fragmentA != null)
  fragmentA.updateUIstatusA();

if (fragmentB != null)
  fragmentB.updateUIstatusB();

}

关于android - getSupportFragmentManager().findFragmentByTag() 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27934085/

相关文章:

android - CoordinatorLayout 不隐藏操作栏

java - 无法通过数据绑定(bind)在 Fragment 上使用 RecyclerView

java - fragment 加载重复值,可能的原因?

java - 在方向更改中更改 fragment 宽度

android - 当响应包含印地语或其他特殊字符时,SAXParser 失败

安卓 AVD + Eclipse : How to remove a directory from Android File Explorer?

android - 如何在Android WebView中呈现大尺寸pdf文档?

android - 如何在viewpager的选项卡上方添加按钮

android - 以编程方式单击上下文操作栏上的“完成”按钮

java - 使用 myid3 库编辑音乐标签