For FragmentManager findFragmentById, return nul, findfragmentbyid

Source: Internet
Author: User

For FragmentManager findFragmentById, return nul, findfragmentbyid

First, let's look at the two generation methods of Fragment.

1. Generate with xml tags

Add an xml tag to the fragment's host activity.

1     <fragment2         android:id="@+id/fragment_newsContent"3         android:name="com.firstcode.section4_news.NewsContentFragment"4         android:layout_width="match_parent"5         android:layout_height="match_parent"/>

The fragment class created by name for you

This method has already generated fragment when the activity is created.

Operations for obtaining fragment instances in an Activity:

1         NewsContentFragment fragment = (NewsContentFragment) getFragmentManager().findFragmentById(R.id.fragment_newsContent);

 

Ii. Dynamic generation using java code

Add FrameLayout to the view file of the fragment host activity for placeholder

1 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"2     android:id="@+id/fragmentContainer"/>

Fragment generation in Activity

1 FragmentManager fm = getFragmentManager (); 2 fm. beginTransaction () 3. add (R. id.FragmentContainer, "The fragment class instance you created ")
. Commit ();

 

Problem Analysis:

I am using FragmentManager. findFragmentById to return nul. This is where I generate fragment in the second way, that is, the real parameter in findFragmentById

I entered the FrameLayout Id, instead of the fragment Id. Therefore, null is returned.

Solution:

1. If fragment is generated statically, use getFragmentManager (). findFragmentById to obtain the fragment instance.

2. If you use java code to dynamically generate fragment, you just need to get a new fragment instance and use getFragmentManager (). findFragmentById.

3. Pay attention to the labels FrameLayout and fragment in the xml file.

 

There is another problem. I also solved this problem by adding text to textview In the fragment view.

 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference

The reason is that the Id parameter in the fragment add generated by the java code fills in the fragment id, so the fragment view is not generated.

 

The best documentation for understanding the two fragment generation methods is google's official Android Training. Below is a Chinese translation.

Add fragment using xml tags

Dynamically add fragment using java code

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.