title: How do I change the content view when a different tab is selected? (Android HoneyCombGallery example related)
body: <p>I am trying to modify the HoneyCombGallery example code to change the displayed view when tabs are changed. In the example provided by Google, the fragments displayed are always the same when you switch tabs. Only the data displayed changes. However, what I want to do is to change the fragments and the classes displayed when tabs are changed.</p>
  
  <p>Here is the code I have modified in the HoneyCombGallery example: (MainActivity.java)</p>
  
  <pre><code>public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
      int nTabSelected = tab.getPosition();
      switch (nTabSelected) {
      case 0:
          setContentView(R.layout.settings_fragment);
          break;
      case 1:
          setContentView(R.layout.main);
          break;
      }
  </code></pre>
  
  <p>everything else is pretty much the same. (also note that, as earlier, MainActivity.onCreate calls setContentView(R.layout.main) - I have not heard that calling setContentView multiple times is not allowed. I intend to do that in each tab click anyway)</p>
  
  <p>And here is the layout that is being inflated in the code (settings_fragment.xml)</p>
  
  <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="horizontal"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:id="@+id/frags2"&gt;
      &lt;fragment class="com.example.android.hcgallery.SettingsFragment"
              android:id="@+id/settings_fragment2"
              android:visibility="gone"
              android:layout_marginTop="?android:attr/actionBarSize"
              android:layout_width="@dimen/titles_size"
              android:layout_height="match_parent" /&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>The "settings fragment" is my class which extends Fragment: (SettingsFragment.java)</p>
  
  <pre><code>public class SettingsFragment extends Fragment {
      private View v;
      @Override
      public View onCreateView(final LayoutInflater inflater,
              final ViewGroup container, final Bundle savedInstanceState) {
  
          if (v != null)
              return v;
          v = inflater.inflate(R.layout.settings_layout, container, false);
          return v;
      }
  }
  </code></pre>
  
  <p>The xml is very straighforward as well (settings_layout.xml): </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/settings_layout" android:layout_width="match_parent"
      android:layout_height="match_parent" android:paddingTop="10dip"&gt;
  
      &lt;TextView android:text="1.0.0" android:id="@+id/tv_version2"
          android:layout_marginTop="?android:attr/actionBarSize"
          android:textSize="15dp" android:textStyle="bold" android:typeface="sans"
          android:layout_height="wrap_content" android:layout_width="200dp"
          android:paddingTop="10dip" android:gravity="right"  &gt;
      &lt;/TextView&gt;
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>This seems straight forward and when the app starts the settings_fragment is visible as the 0th tab is selected by default. When we select the next tab I expect that setContentView(R.layout.main) is executed and I will see the main layout (this is the same as in the Google example: no modifications). However, what I get is this exception:</p>
  
  <pre><code>12-15 16:49:28.501: ERROR/AndroidRuntime(31485): FATAL EXCEPTION: main
  12-15 16:49:28.501: ERROR/AndroidRuntime(31485): android.view.InflateException: Binary XML file line #23: Error inflating class fragment
  12-15 16:49:28.501: ERROR/AndroidRuntime(31485):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:688)
  12-15 16:49:28.501: ERROR/AndroidRuntime(31485):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:724)
  12-15 16:49:28.501: ERROR/AndroidRuntime(31485):     at android.view.LayoutInflater.inflate(LayoutInflater.java:479)
  12-15 16:49:28.501: ERROR/AndroidRuntime(31485):     at android.view.LayoutInflater.inflate(LayoutInflater.java:391)
  12-15 16:49:28.501: ERROR/AndroidRuntime(31485):     at android.view.LayoutInflater.inflate(LayoutInflater.java:347)
  12-15 16:49:28.501: ERROR/AndroidRuntime(31485):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:224)
  12-15 16:49:28.501: ERROR/AndroidRuntime(31485):     at android.app.Activity.setContentView(Activity.java:1777)
  12-15 16:49:28.501: ERROR/AndroidRuntime(31485):     at com.example.android.hcgallery.MainActivity.onTabSelected(MainActivity.java:109)
  12-15 16:49:28.501: ERROR/AndroidRuntime(31485):     at com.android.internal.app.ActionBarImpl.selectTab(ActionBarImpl.java:462)
  12-15 16:49:28.501: ERROR/AndroidRuntime(31485):     at com.android.internal.app.ActionBarImpl$TabImpl.select(ActionBarImpl.java:787)
  12-15 16:49:28.501: ERROR/AndroidRuntime(31485):     at com.android.internal.widget.ActionBarView$TabClickListener.onClick(ActionBarView.java:950)
  12-15 16:49:28.501: ERROR/AndroidRuntime(31485):     at android.view.View.performClick(View.java:3100)
  12-15 16:49:28.501: ERROR/AndroidRuntime(31485):     at android.view.View$PerformClick.run(View.java:11644)
  12-15 16:49:28.501: ERROR/AndroidRuntime(31485):     at android.os.Handler.handleCallback(Handler.java:587)
  12-15 16:49:28.501: ERROR/AndroidRuntime(31485):     at android.os.Handler.dispatchMessage(Handler.java:92)
  12-15 16:49:28.501: ERROR/AndroidRuntime(31485):     at android.os.Looper.loop(Looper.java:126)
  12-15 16:49:28.501: ERROR/AndroidRuntime(31485):     at android.app.ActivityThread.main(ActivityThread.java:3997)
  12-15 16:49:28.501: ERROR/AndroidRuntime(31485):     at java.lang.reflect.Method.invokeNative(Native Method)
  12-15 16:49:28.501: ERROR/AndroidRuntime(31485):     at java.lang.reflect.Method.invoke(Method.java:491)
  12-15 16:49:28.501: ERROR/AndroidRuntime(31485):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
  12-15 16:49:28.501: ERROR/AndroidRuntime(31485):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
  12-15 16:49:28.501: ERROR/AndroidRuntime(31485):     at dalvik.system.NativeStart.main(Native Method)
  12-15 16:49:28.501: ERROR/AndroidRuntime(31485): Caused by: java.lang.IllegalArgumentException: Binary XML file line #23: Duplicate id 0x7f09000a, tag null, or parent id 0x7f090009 with another fragment for com.example.android.hcgallery.TitlesFragment
  12-15 16:49:28.501: ERROR/AndroidRuntime(31485):     at android.app.Activity.onCreateView(Activity.java:4095)
  12-15 16:49:28.501: ERROR/AndroidRuntime(31485):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:664)
  12-15 16:49:28.501: ERROR/AndroidRuntime(31485):     ... 21 more
  </code></pre>
  
  <p>The error seems to be "android.view.InflateException: Binary XML file line #23: Error inflating class fragment" </p>
  
  <p>What does this mean? Can I not do a setContentView to change the fragments and layouts as I wish when different tabs are selected?
  If this is not the right way to have different layouts for different tabs, what is the right way to do it?</p>
  
  <p>I have the entire source code zipped uploaded to HoneycombGallery_Modified.zip - <a href="https://docs.google.com/open?id=0B0FHtQOpOYx6YWFiMDcxYmEtOGFkMy00NWU2LTk2MzMtZWY0YzdmZWUzMDU0" rel="nofollow">https://docs.google.com/open?id=0B0FHtQOpOYx6YWFiMDcxYmEtOGFkMy00NWU2LTk2MzMtZWY0YzdmZWUzMDU0</a> (Click on "File->Download original" if you want to download the whole zip file)</p>