Android Fragment Nested Use error

Source: Internet
Author: User

Each time the new SDK creates an activity, it automatically generates
<pre name= "code" class= "Java" >public static class Placeholderfragment extends Fragment

Fragment module, the nested fragment code on the basis of the module is as follows:

<pre name= "code" class= "Java" >public static class Placeholderfragment extends Fragment {static Fragmentmanager FM; Public placeholderfragment () {   fm=getchildfragmentmanager ();} @Override public  void OnCreate (Bundle savedinstancestate) {      super.oncreate (savedinstancestate);       }  @Overridepublic View Oncreateview (Layoutinflater inflater, ViewGroup container,bundle savedinstancestate) {view Rootview=layoutinflater.from (Getactivity ()). Inflate (R.layout.tab, NULL), if (Fm.findfragmentbytag ("haha") ==null) { Fragment1 f1=new Fragment1 (); Fm.begintransaction (). Add (r.id.frame_tab,f1, "haha"). commit ();} return Rootview;}

The resulting error is as follows:


In short it means that acitivity was destroyed.

Reason:

Fm=getchildfragmentmanager (); in <span style= "font-family:arial, Helvetica, Sans-serif;" Called in the >placeholderfragment constructor, when it has not yet created a oncreate, it naturally gets less than fragment and activity, so writes it to </span>
<span style= "font-family:arial, Helvetica, Sans-serif;" ></span><pre name= "code" class= "java" ><pre name= "code" class= "java" >public void OnCreate (Bundle Savedinstancestate) {      super.oncreate (savedinstancestate);   Fm=getchildfragmentmanager ();    }  

Error resolution, we must pay attention to the fragment and activity life cycle.

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.