Iljmall encountered fragment nesting issues during project: illegalargumentexception:binary XML file line #23: Duplicate ID

Source: Internet
Author: User

    • Scene: Error exits when you click "Categorize" and return to "home"
    • Bug Description: caused by:java.lang.IllegalArgumentException:Binary XML file line #23: Duplicate ID 0x7f0d0054, tag null, or Paren T ID 0xFFFFFFFF with another fragment for Com.example.sxq123.iljmall.FragmentCatagorySpace
    • Framgment_home.xml
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"android:orientation= "vertical" android:layout_width= "match_parent"Android:layout_height= "Match_parent" > <ScrollView android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:layout_weight= "1" > <LinearLayout android:orientation= "Vertical"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content" > <TextView android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "@string/home"/><Fragment Android:id= "@+id/fragment_space"Android:name= "Com.example.sxq123.iljmall.FragmentCatagorySpace"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"/></linearlayout></scrollview></linearlayout>

    • Fragmenthome.java
 Public classFragmenthomeextendsFragment {Privateview view;    Fragmentcatagoryspace Fragmentcatagoryspace; @Override PublicView Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinsatacestate) {View= Inflater.inflate (R.layout.fragment_home,NULL); LOG.D ( This. GetClass (). GetName (), "Oncreateview () Begin");        Initchildfragment (); returnview; }    Private voidinitchildfragment () {LOG.D ("-------------------", "Init space"); Fragmentcatagoryspace=(Fragmentcatagoryspace) Getchildfragmentmanager (). Findfragmentbyid (R.id.fragment_space); if(Fragmentcatagoryspace! =NULL) {LOG.D ("----------------", "Init space success and no null"); }    }}

    • Cause of the problem
Fragmentmanager only Remove and add these frament when the project is replaced between different frament in http://www.tuicool.com/articles/FNVN3i activity, However these frament inside themselves loaded frament (here is our fragmentcatagoryspace) is not removed. Obviously this is a flaw!  Since the latter frament (Fragmentcatagoryspace) is obviously dependent on the frament with his father, the remove should be recursive at the same time. So how to solve this problem! It is clear that the frament loaded inside him is removed when you don't need this frament (fragmenthome)! This operation can solve the problem by fragment the Oncreateview () inflate layout before you remove it! For example, the remove transaction is placed in the parent fragment's Ondestroyview () execution
    • The revised Code
 Public classFragmenthomeextendsFragment {Privateview view;    Fragmentcatagoryspace Fragmentcatagoryspace; @Override PublicView Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinsatacestate) {View= Inflater.inflate (R.layout.fragment_home,NULL); LOG.D ( This. GetClass (). GetName (), "Oncreateview () Begin");        Initchildfragment (); returnview; } @Override Public voidOndestroyview () {Super. Ondestroyview (); if(Fragmentcatagoryspace! =NULL) {LOG.D ("-------------------", "Space no null"); Fragmentmanager Fragmentmanager=Getfragmentmanager (); if(Fragmentmanager! =NULL&&!fragmentmanager.isdestroyed ()) {            FinalFragmenttransaction fragmenttransaction =fragmentmanager.begintransaction (); if(Fragmenttransaction! =NULL) {fragmenttransaction.remove (fragmentcatagoryspace). commit (); //commit () and Commitallowingstateloss () are sent first to the main thread task queue when the main thread is ready to execute again, asynchronously. // //If you do not execute the Remove child FRAGMETN transaction before inflate, exception of the duplicate ID will occur!!!//fragmenttransaction.commit ();//A commit () can be placed in Oncreateview, but occasionally onsaveinstancestate () cannot be executed after a conflictFragmenttransaction.commitallowingstateloss (); //executes the transaction in the task queue immediately, not asynchronously!!!!!!!!!!!!!!! Key!!!!!!!!!!!!!!!!!!!!//prevents the remove transaction from being added to the main thread task queue, which can be done asynchronously until the parent fragment re-executes Oncreateview ()//an exception that duplicate ID will also occur before the execution is complete.//If the remove food is placed in the parent fragment's onsaveinstancestate (), the onsaveinstancestate () call does not//is that every fragment life cycle will be called (???? ), so duplicate ID exceptions occasionally occurfragmentmanager.executependingtransactions (); LOG.D ("------------------", "Space Destroy"); }        }    }}    Private voidinitchildfragment () {LOG.D ("-------------------", "Init space"); Fragmentcatagoryspace=(Fragmentcatagoryspace) Getchildfragmentmanager (). Findfragmentbyid (R.id.fragment_space); if(Fragmentcatagoryspace! =NULL) {LOG.D ("----------------", "Init space success and no null"); }    }}

Iljmall encountered fragment nesting issues during project: illegalargumentexception:binary XML file line #23: Duplicate ID

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.