title: Exception when inflating a layout containing fragments
body: <p>I'm developing a <a href="https://play.google.com/store/apps/details?id=org.dobbo.colour" rel="nofollow">colour wheel app</a> for fun.  Version 0.5 has a palette made up of four fragments, one for each of the possible colour targets on the colour wheel.  The fragments' UI elements are then hidden or shown depending upon the mode of the colour wheel.  "Monochromatic" mode needs only one of the fragments, where as "Accented Anaogic" needs all four.  This works find as the fragments are built and displayed as part of the <code>onCreate()</code> method, and are never touched thereafter.</p>
  
  <p>For the next version to improve the layout I have designed four "include" XML layout files, one using only one instance of the fragment, one using two, and so on.  The master XML layout file now has a <code>FrameLayout</code> as the point where I replace the appropriate "include" XML layout file when the mode is changed.</p>
  
  <p>Here is the code fragment that performs the change:</p>
  
  <pre><code>private void changeLayout(int layoutID) {
      FragmentManager fMgr = getFragmentManager();
      FragmentTransaction transaction = fMgr.beginTransaction();
  
      LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  
      FrameLayout frame = (FrameLayout) findViewById(R.id.paletteContainer);
      frame.removeAllViews();
  
      View view = inflater.inflate(layoutID, frame, false);
      frame.addView(view);
      transaction.commit();
  
      pFrag = (SwatchFragment.Primary) fMgr.findFragmentById(R.id.base);
      a1Frag = (SwatchFragment.FirstAlternate) fMgr.findFragmentById(R.id.alternate1);
      a2Frag = (SwatchFragment.SecondAlternate) fMgr.findFragmentById(R.id.alternate2);
      cFrag = (SwatchFragment.Complementary) fMgr.findFragmentById(R.id.complementary);    
  }
  </code></pre>
  
  <p>It exceptions as given below because one or more of the fragments are being reused.  So what is the correct method for replacing part of the UI which contains fragments?</p>
  
  <pre><code>E/WheelActivityandroid.view.InflateException: Binary XML file line #11: Error inflating class Navigation item selected: pos=1, id=0x00000001
  android.view.InflateException: Binary XML file line #11: Error inflating class fragment
          at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) ~[na:0.0]
          at android.view.LayoutInflater.rInflate(LayoutInflater.java:746) ~[na:0.0]
          at android.view.LayoutInflater.inflate(LayoutInflater.java:489) ~[na:0.0]
          at android.view.LayoutInflater.inflate(LayoutInflater.java:396) ~[na:0.0]
          at org.dobbo.colour.activity.WheelActivity.changeLayout(WheelActivity.java:321) ~[na:0.0]
          at org.dobbo.colour.activity.WheelActivity.setMode(WheelActivity.java:307) ~[na:0.0]
          at org.dobbo.colour.activity.WheelActivity.onNavigationItemSelected(WheelActivity.java:181) ~[na:0.0]
          at com.android.internal.widget.ActionBarView$1.onItemSelected(ActionBarView.java:148) ~[na:0.0]
          at android.widget.AdapterView.fireOnSelected(AdapterView.java:892) ~[na:0.0]
          at android.widget.AdapterView.access$200(AdapterView.java:49) ~[na:0.0]
          at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:860) ~[na:0.0]
          at android.os.Handler.handleCallback(Handler.java:725) ~[na:0.0]
          at android.os.Handler.dispatchMessage(Handler.java:92) ~[na:0.0]
          at android.os.Looper.loop(Looper.java:137) ~[na:0.0]
          at android.app.ActivityThread.main(ActivityThread.java:5039) ~[na:0.0]
          at java.lang.reflect.Method.invokeNative(Native Method) ~[na:0.0]
          at java.lang.reflect.Method.invoke(Method.java:511) ~[na:0.0]
          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) ~[na:0.0]
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) ~[na:0.0]
          at dalvik.system.NativeStart.main(Native Method) ~[na:0.0]
  Caused by: java.lang.IllegalArgumentException: Binary XML file line #11: Duplicate id 0x7f0a000b, tag null, or parent id 0x7f0a0010 with anoth
  er fragment for org.dobbo.colour.fragment.SwatchFragment$Primary
          at android.app.Activity.onCreateView(Activity.java:4722) ~[na:0.0]
          at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680) ~[na:0.0]
          ...
  </code></pre>