android - findFragmentByTag() 在使用 replace() 方法执行 FragmentTransaction 后返回 null

标签 android android-fragments fragmenttransaction

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

应用启动时,显示在left_container中加载的fragmentA在right_container中的fragmentC

如果您按下 fragmentA 中的按钮,则 FragmentTransaction 会将 FragmentC 更改为 FragmentB

目前一切正常。 但是当我尝试使用 findFragmentByTag() 获取对加载的fragmentB 的引用时出现了问题,因为它返回null。我在 FragmentTransaction 中使用了方法 replace 并用 commit() 完成了它,但是无法调用 FragmentB 方法。我的代码:

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();


    }
}

FragmentB.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!");
}

}

activity_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>

fragment_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()findFragmentByTag() 方法获取 fragment 。

关于android - findFragmentByTag() 在使用 replace() 方法执行 FragmentTransaction 后返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20825600/

相关文章:

android - TabLayout 中的 fragment 仅在用户滑动 Android 时加载

来自 Fragment 选项卡的 Android 加载 Activity

android - 无法弄清楚如何使用 ImageViewTouch Hook

android - 在 2 个应用程序之间连续处理 Intent

android - fragment 交易的更好方法

java - 第二次调用 FragmentTransaction.replace() 时出现 Android Fragment 转换错误

java - Android 与 Eclipse 不兼容?

android - 我应该使用哪个 firebaseUI 库作为 androidX 中的实时数据库?

android - android支持库必须使用完全相同的版本吗?

android - Google Play 服务版本自动更改为 11.8