gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 02:25:38 25 4
gpt4 key购买 nike

我正在尝试通过获取容器中的当前 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com