title: Error inflating class fragment | fragment did not create a view
body: <p>I'm having this problem:</p>
  
  <p><strong>android.view.InflateException: Binary XML file line #31: Error inflating class fragment
  Fragment com.myapp.mgm.QueryFragment did not create a view.</strong></p>
  
  <p>This problem is only happening when in tablet mode. If app runs on phone, it works ok.</p>
  
  <p>I have this <code>layout-large</code> xml file (fragments.xml):</p>
  
  <pre><code>&lt;android.support.v4.widget.DrawerLayout
          xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:id="@+id/drawer_layout"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:background="@color/lightgrey"&gt;
  &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:background="@color/lightgrey"
                android:baselineAligned="false"&gt;
  
      &lt;fragment
              android:name="com.myapp.mgm.QueryFragment"
              android:id="@+id/fragment1"
              android:layout_weight="1"
              android:layout_width="0dp"
              android:layout_height="match_parent"
              tools:layout="@layout/nu"/&gt;
  
      &lt;fragment
              android:name="com.myapp.mgm.Infodata"
              android:id="@+id/fragment2"
              android:layout_weight="2"
              android:layout_width="0dp"
              android:layout_height="match_parent"
              tools:layout="@layout/maindata"/&gt;
  
  &lt;/LinearLayout&gt;
          &lt;ListView
                  android:id="@+id/list_slidermenu"
                  android:layout_width="240dp"
                  android:layout_height="match_parent"
                  android:layout_gravity="start"
                  android:choiceMode="singleChoice"
                  android:divider="@color/orange"
                  android:dividerHeight="1dp"
                  android:listSelector="@drawable/menulist_selector"
                  android:background="@color/lblack"/&gt;
  &lt;/android.support.v4.widget.DrawerLayout&gt;
  </code></pre>
  
  <p>As you can see I have two fragments and one Navigation drawer on my layout.</p>
  
  <p>The class that puts all of this together is this:</p>
  
  <pre><code>public class Fragments extends ActionBarActivity implements QueryFragment.OnQuerySelectedListener {
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.fragments);
  
          slidingMenu();
  
          if (findViewById(R.id.fragmentsboth) != null) {
              if (savedInstanceState != null) {
                  return;
              }
              QueryFragment firstFragment = new QueryFragment();
              getSupportFragmentManager().beginTransaction().add(R.id.fragmentsboth, firstFragment).commit();
          }
      }
  }
  </code></pre>
  
  <p>And my <code>QueryFragment</code> class is this:</p>
  
  <pre><code>public class QueryFragment extends ListFragment {
      public interface OnQuerySelectedListener {
          public void onItemSelected(Bundle itemSelectedData);
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          operation = Fragments.operation;
          if (operation == 0) {
              layoutToShow = inflater.inflate(R.layout.querylist, container, false);
              ...
          } else if (operation == 1) {
              layoutToShow = inflater.inflate(R.layout.querylist, container, false);
              ...
          } else if (operation == 2) {
              layoutToShow = inflater.inflate(R.layout.search, container, false);
              ...
          } else if (operation == 3) {
              layoutToShow = inflater.inflate(R.layout.nu, container, false);
              ...
          }
          return layoutToShow;
      }
  </code></pre>
  
  <p>}</p>
  
  <p>And my <code>InfoData</code> class as this stuff:</p>
  
  <pre><code>public class Infodata extends Fragment implements OnSeekBarChangeListener, OnItemSelectedListener {
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          setHasOptionsMenu(true);
  
          View itemDataLayout = inflater.inflate(R.layout.maindata, container, false);
          ....
  
          if (savedInstanceState != null &amp;&amp; savedInstanceState.size()&gt;=2) {
              layoutType = savedInstanceState.getInt("layout");
              comesFromSavedState = true;
              itemData = savedInstanceState;
          }
          return itemDataLayout;
      }
  
      @Override
      public void onStart() {
          super.onStart();
  
          Bundle args = getArguments();
          if (args != null) {
              populateLayoutWithData(1, args);
          } else if(comesFromSavedState){
              populateLayoutWithData(2, itemData);
          }
      }
  
      @Override
      public void onSaveInstanceState(Bundle outState) {
          super.onSaveInstanceState(outState);
          //Saves stuff (data) in a bundle to restore after screen rotation
      }   
  }
  </code></pre>
  
  <p>Basically I have a <code>listview</code> on the left fragment (<code>QueryFragment</code>) and after the user selects one of the items (a cursor with data), the right fragment (<code>InfoData</code>) is populated with the extracted data of this cursor.
  If I select an intem from the left fragment and it's data its populated on the right fragment, since i'm saving data on <code>onSaveInstanceState</code>, if I rotate the screen, everything is working OK, that is, data is reloaded on the right fragment.</p>
  
  <p>Now, my Navigadion Drawer as 4 options. Each one of this options, loads a <code>listView</code> on the left fragment, and then, as before, if the user selects one of those items, it's data is loaded on the right fragment.</p>
  
  <p>Here is the code that handles this in my <code>Fragments</code> class:</p>
  
  <pre><code>private void selectItem(int position) {
      int typeOfLayout;
      mDrawerList.setItemChecked(position, true);
      mDrawerLayout.closeDrawer(mDrawerList);
      QueryFragment newFragment;
      FragmentTransaction transaction;
  
      ViewGroup parentView = (ViewGroup) findViewById(R.id.fragmentsboth);
  
      if (parentView != null) {
          typeOfLayout = R.id.fragmentsboth;
      } else {
          typeOfLayout = R.id.fragment1;
      }
  
      switch (position) {
          case 0:
              Intent i = new Intent(this, NutriData.class);
              i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
              startActivity(i);
              finish();
              break;
          case 1:
              newFragment = new QueryFragment();
              operation = 0;
              transaction = getSupportFragmentManager().beginTransaction();
              transaction.replace(typeOfLayout, newFragment).commit();
              break;
          case 2:
              newFragment = new QueryFragment();
              operation = 1;
              transaction = getSupportFragmentManager().beginTransaction();
              transaction.replace(typeOfLayout, newFragment).commit();
              break;
          case 3:
              newFragment = new QueryFragment();
              operation = 2;
              transaction = getSupportFragmentManager().beginTransaction();
              transaction.replace(typeOfLayout, newFragment).commit();
              break;
          case 4:
              newFragment = new QueryFragment();
              operation = 3;
              transaction = getSupportFragmentManager().beginTransaction();
              transaction.replace(typeOfLayout, newFragment).commit();
              break;
      }
  }
  </code></pre>
  
  <p>Imagine this senario:</p>
  
  <p>I got a <code>listView</code>with item's on the left fragment and then select one of these items. Then, it's data is passed to the right fragment that put's it on the right place. If I rotate the screen, data is saved and after rotation is reloaded on the right fragment. Now, I open the navigation drawer and select one of the options (1,2,3 or 4) and it's corresponding <code>listView</code> is loaded on the left fragment. The right fragment keeps showing the data from the first item the user as selected. Now, I press on a new item on the left fragment and it's data is loaded on the right fragment. Sweet. It's working as I wanted to. The problem is if I now rotate my screen. I'm getting this error:</p>
  
  <pre><code>01-01 15:51:16.356    2569-2569/com.myapp.mgm E/AndroidRuntime﹕ FATAL EXCEPTION: main
      java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp.mgm/com.myapp.mgm.Fragments}: android.view.InflateException: Binary XML file line #31: Error inflating class fragment
              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
              at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3740)
              at android.app.ActivityThread.access$700(ActivityThread.java:141)
              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1262)
              at android.os.Handler.dispatchMessage(Handler.java:99)
              at android.os.Looper.loop(Looper.java:137)
              at android.app.ActivityThread.main(ActivityThread.java:5103)
              at java.lang.reflect.Method.invokeNative(Native Method)
              at java.lang.reflect.Method.invoke(Method.java:525)
              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
              at dalvik.system.NativeStart.main(Native Method)
       Caused by: android.view.InflateException: Binary XML file line #31: Error inflating class fragment
              at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
              at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
              at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
              at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
              at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
              at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
              at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:267)
              at android.app.Activity.setContentView(Activity.java:1895)
              at android.support.v7.app.ActionBarActivity.superSetContentView(ActionBarActivity.java:208)
              at android.support.v7.app.ActionBarActivityDelegateICS.setContentView(ActionBarActivityDelegateICS.java:111)
              at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:76)
              at com.myapp.mgm.Fragments.onCreate(Fragments.java:27)
              at android.app.Activity.performCreate(Activity.java:5133)
              at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
              at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3740)
              at android.app.ActivityThread.access$700(ActivityThread.java:141)
              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1262)
              at android.os.Handler.dispatchMessage(Handler.java:99)
              at android.os.Looper.loop(Looper.java:137)
              at android.app.ActivityThread.main(ActivityThread.java:5103)
              at java.lang.reflect.Method.invokeNative(Native Method)
              at java.lang.reflect.Method.invoke(Method.java:525)
              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
              at dalvik.system.NativeStart.main(Native Method)
       Caused by: java.lang.IllegalStateException: Fragment com.myapp.mgm.QueryFragment did not create a view.
              at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:308)
              at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
              at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
              at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
              at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
              at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
              at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
              at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:267)
              at android.app.Activity.setContentView(Activity.java:1895)
              at android.support.v7.app.ActionBarActivity.superSetContentView(ActionBarActivity.java:208)
              at android.support.v7.app.ActionBarActivityDelegateICS.setContentView(ActionBarActivityDelegateICS.java:111)
              at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:76)
              at com.myapp.mgm.Fragments.onCreate(Fragments.java:27)
              at android.app.Activity.performCreate(Activity.java:5133)
              at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
              at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3740)
              at android.app.ActivityThread.access$700(ActivityThread.java:141)
              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1262)
              at android.os.Handler.dispatchMessage(Handler.java:99)
              at android.os.Looper.loop(Looper.java:137)
              at android.app.ActivityThread.main(ActivityThread.java:5103)
              at java.lang.reflect.Method.invokeNative(Native Method)
              at java.lang.reflect.Method.invoke(Method.java:525)
              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
              at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>Can someone please explain me what I'm doing wrong?</p>