Note that there are some explanatory texts on larger screens.

plurals
  1. POfindFragmentByTag() returns null after perform a FragmentTransaction using replace() method
    primarykey
    data
    text
    <p>My Android app consists three <strong>fragments: A, B and C</strong>. They're loaded in the two containers defined in the <code>MainActivity</code> layout. </p> <p>When the app is started, it shows the <strong>fragmentA loaded in the left_container</strong> and the <strong>fragmentC in the right_container</strong>. </p> <p>If you press the button in the <strong>fragmentA</strong>, a <code>FragmentTransaction</code> changes <strong>FragmentC</strong> by <strong>FragmentB</strong>. </p> <p>At the moment everything OK. <strong>But the trouble appears when I try to get a reference to the loaded fragmentB using</strong> <code>findFragmentByTag()</code>, because it returns <code>null</code>. I've used the method replace in the <code>FragmentTransaction</code> and I've finished it with <code>commit()</code>, but there isn't way to call <strong>FragmentB</strong> method. My code:</p> <p>MainActivity.java:</p> <pre><code> 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(); } } </code></pre> <p>FragmentB.java:</p> <pre><code>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!"); } } </code></pre> <p>activity_main.xml:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" &gt; &lt;FrameLayout android:id="@+id/left_container" android:layout_width="0px" android:layout_weight="50" android:layout_height="match_parent"/&gt; &lt;FrameLayout android:id="@+id/right_container" android:layout_width="0px" android:layout_weight="50" android:layout_height="match_parent"/&gt; &lt;/LinearLayout&gt; </code></pre> <p>fragment_b.xml:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;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"&gt; &lt;TextView android:id="@+id/text_fragment_b" android:text="It doesn't works!" android:layout_width="match_parent" android:layout_height="wrap_content"/&gt; &lt;/LinearLayout&gt; </code></pre> <p>Please help me! I'm a beginner in Android development! </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload