findFragmentByTag()使用替代返回null后执行FragmentTransaction()方法方法、findFragmentByTag、null、FragmentTransaction

2023-09-05 08:35:08 作者:萌妹乔治

我的Andr​​oid应用程序包含三个片段:A,B和C 。他们在 MainActivity 布局定义的两个集装箱装载。

My Android app consists three fragments: A, B and C. They're loaded in the two containers defined in the MainActivity layout.

当应用程序启动时,它显示在碎裂的left_container 加载和 fragmentC在right_container 。

When the app is started, it shows the fragmentA loaded in the left_container and the fragmentC in the right_container.

如果您preSS在碎裂按钮,一个 FragmentTransaction 修改 FragmentC 按 FragmentB 。

If you press the button in the fragmentA, a FragmentTransaction changes FragmentC by FragmentB.

目前,一切就OK了。 但是当我尝试使用得到的引用,加载fragmentB出现的麻烦 findFragmentByTag(),因为它返回。我用的方法替换 FragmentTransaction 然后我把完成了它的commit(),但有ISN T方法来调用 FragmentB 方式。我的code:

At the moment everything OK. But the trouble appears when I try to get a reference to the loaded fragmentB using findFragmentByTag(), because it returns null. I've used the method replace in the FragmentTransaction and I've finished it with commit(), but there isn't way to call FragmentB method. My code:

MainActivity.java:

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:

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:

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:

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开发的初学者!

Please help me! I'm a beginner in Android development!

推荐答案

我已经解决了!我叫 getSupportFragmentManager()。executePendingTransactions()做交易后,它的工作!调用该方法后,我可以同时使用 findFragmentById() findFragmentByTag() mehthods片段。

I've fixed it! I called getSupportFragmentManager().executePendingTransactions() after doing the transaction and it worked! After calling that method I can get the fragment using both findFragmentById() and findFragmentByTag() mehthods.