title: Android - android.view.InflateException: Binary XML file line #9: Error inflating class fragment
body: <p>I got this error message state <code>android.view.InflateException: Binary XML file line #9: Error inflating class fragment</code>. Is it because of i'm not extending it into <code>fragment</code> in <code>MessagesFragment</code>? By the way, my app using <code>TabHost</code>.</p>
  
  <p>FirstFragment.java</p>
  
  <pre><code>package com.xxxxx.yyyyy.match;
  
  import com.xxxxx.yyyyy.R;
  
  import android.os.Bundle;
  import android.support.v4.app.Fragment;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  
  public class FirstFragment extends Fragment {
  
      public FirstFragment() {
      }
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
              Bundle savedInstanceState) {
          View v = LayoutInflater.from(getActivity()).inflate(R.layout.first_fragment,
                  null);
      return v;
      }
  }
  </code></pre>
  
  <p>first_fragment.xml</p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  android:background="@android:color/darker_gray"
  xmlns:tools="http://schemas.android.com/tools"
  tools:context=".FirstFragment" &gt;
  
  &lt;fragment 
          android:name="com.xxxxx.yyyyy.chat.MessagesFragment"
          android:id="@+id/msg_list"
          android:layout_width="match_parent"
          android:layout_height="0dip"
          android:layout_weight="1" /&gt;
  
   &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>MessagesFragment.java</p>
  
  <pre><code>package com.xxxxx.yyyyy.chat;
  
  import java.text.DateFormat;
  import java.text.ParseException;
  import java.text.SimpleDateFormat;
  import java.util.Date;
  
  import com.xxxxx.yyyyy.R;
  import com.xxxxx.yyyyy.library.chat.DataProvider;
  
  import android.app.Activity;
  import android.app.ListFragment;
  import android.app.LoaderManager;
  import android.content.CursorLoader;
  import android.content.Loader;
  import android.database.Cursor;
  import android.os.Bundle;
  import android.view.Gravity;
  import android.view.View;
  import android.widget.LinearLayout;
  import android.widget.SimpleCursorAdapter;
  import android.widget.TextView;
  
  public class MessagesFragment extends ListFragment implements LoaderManager.LoaderCallbacks&lt;Cursor&gt; {
  
      private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      private static final DateFormat[] df = new DateFormat[] {
          DateFormat.getDateInstance(), DateFormat.getTimeInstance()};
  
      private OnFragmentInteractionListener mListener;
      private SimpleCursorAdapter adapter;
      private Date now;
  
      @Override
      public void onAttach(Activity activity) {
          super.onAttach(activity);
          try {
              mListener = (OnFragmentInteractionListener) activity;
          } catch (ClassCastException e) {
              throw new ClassCastException(activity.toString() + " must implement OnFragmentInteractionListener");
          }
      }   
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          now = new Date();
  
          adapter = new SimpleCursorAdapter(getActivity(), 
                  R.layout.chat_list_item, 
                  null, 
                  new String[]{DataProvider.COL_MSG, DataProvider.COL_AT}, 
                  new int[]{R.id.text1, R.id.text2},
                  0);
  
          adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
  
              @Override
              public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
                  switch(view.getId()) {
                  case R.id.text1:
                      LinearLayout root = (LinearLayout) view.getParent().getParent();
                      if (cursor.getString(cursor.getColumnIndex(DataProvider.COL_FROM)) == null) {
                          root.setGravity(Gravity.RIGHT);
                          root.setPadding(50, 10, 10, 10);
                      } else {
                          root.setGravity(Gravity.LEFT);
                          root.setPadding(10, 10, 50, 10);
                      }
                      break;
  
                  case R.id.text2:
                      TextView tv = (TextView) view;
                      tv.setText(getDisplayTime(cursor.getString(columnIndex)));
                      return true;                    
                  }
                  return false;
              }
          });     
  
          setListAdapter(adapter);
      }   
  
      @Override
      public void onActivityCreated(Bundle savedInstanceState) {
          super.onActivityCreated(savedInstanceState);
  
          getListView().setDivider(null);
  
          Bundle args = new Bundle();
          args.putString(DataProvider.COL_EMAIL, mListener.getProfileEmail());
          getLoaderManager().initLoader(0, args, this);
      }
  
      @Override
      public void onDetach() {
          super.onDetach();
          mListener = null;
      }
  
      public interface OnFragmentInteractionListener {
          public String getProfileEmail();
      }
  
      private String getDisplayTime(String datetime) {
          try {
              Date dt = sdf.parse(datetime);
              if (now.getYear()==dt.getYear() &amp;&amp; now.getMonth()==dt.getMonth() &amp;&amp; now.getDate()==dt.getDate()) {
                  return df[1].format(dt);
              }
              return df[0].format(dt);
          } catch (ParseException e) {
              return datetime;
          }
      }
  
      //----------------------------------------------------------------------------
  
      @Override
      public Loader&lt;Cursor&gt; onCreateLoader(int id, Bundle args) {
          String profileEmail = args.getString(DataProvider.COL_EMAIL);
          CursorLoader loader = new CursorLoader(getActivity(), 
                  DataProvider.CONTENT_URI_MESSAGES, 
                  null, 
                  DataProvider.COL_FROM + " = ? or " + DataProvider.COL_TO + " = ?",
                  new String[]{profileEmail, profileEmail}, 
                  DataProvider.COL_AT + " DESC"); 
          return loader;
      }
  
      @Override
      public void onLoadFinished(Loader&lt;Cursor&gt; loader, Cursor data) {
          adapter.swapCursor(data);
      }
  
      @Override
      public void onLoaderReset(Loader&lt;Cursor&gt; loader) {
          adapter.swapCursor(null);
      }
  
  }
  </code></pre>
  
  <p>Output from logcat</p>
  
  <pre><code>03-06 16:52:05.729: E/AndroidRuntime(5419): FATAL EXCEPTION: main
  03-06 16:52:05.729: E/AndroidRuntime(5419): android.view.InflateException: Binary XML file line #9: Error inflating class fragment
  03-06 16:52:05.729: E/AndroidRuntime(5419):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
  03-06 16:52:05.729: E/AndroidRuntime(5419):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
  03-06 16:52:05.729: E/AndroidRuntime(5419):     at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
  03-06 16:52:05.729: E/AndroidRuntime(5419):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
  03-06 16:52:05.729: E/AndroidRuntime(5419):     at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
  03-06 16:52:05.729: E/AndroidRuntime(5419):     at com.xxxxx.yyyyy.FirstFragment.onCreateView(FirstFragment.java:38)
  03-06 16:52:05.729: E/AndroidRuntime(5419):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
  03-06 16:52:05.729: E/AndroidRuntime(5419):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
  03-06 16:52:05.729: E/AndroidRuntime(5419):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
  03-06 16:52:05.729: E/AndroidRuntime(5419):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
  03-06 16:52:05.729: E/AndroidRuntime(5419):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
  03-06 16:52:05.729: E/AndroidRuntime(5419):     at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:454)
  03-06 16:52:05.729: E/AndroidRuntime(5419):     at android.os.Handler.handleCallback(Handler.java:730)
  03-06 16:52:05.729: E/AndroidRuntime(5419):     at android.os.Handler.dispatchMessage(Handler.java:92)
  03-06 16:52:05.729: E/AndroidRuntime(5419):     at android.os.Looper.loop(Looper.java:137)
  03-06 16:52:05.729: E/AndroidRuntime(5419):     at android.app.ActivityThread.main(ActivityThread.java:5136)
  03-06 16:52:05.729: E/AndroidRuntime(5419):     at java.lang.reflect.Method.invokeNative(Native Method)
  03-06 16:52:05.729: E/AndroidRuntime(5419):     at java.lang.reflect.Method.invoke(Method.java:525)
  03-06 16:52:05.729: E/AndroidRuntime(5419):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
  03-06 16:52:05.729: E/AndroidRuntime(5419):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
  03-06 16:52:05.729: E/AndroidRuntime(5419):     at dalvik.system.NativeStart.main(Native Method)
  03-06 16:52:05.729: E/AndroidRuntime(5419): Caused by: java.lang.ClassCastException: com.xxxxx.yyyyy.HomeActivity@419d4fa8 must implement OnFragmentInteractionListener
  03-06 16:52:05.729: E/AndroidRuntime(5419):     at com.xxxxx.yyyyy.chat.MessagesFragment.onAttach(MessagesFragment.java:49)
  03-06 16:52:05.729: E/AndroidRuntime(5419):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:848)
  03-06 16:52:05.729: E/AndroidRuntime(5419):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1039)
  03-06 16:52:05.729: E/AndroidRuntime(5419):     at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1141)
  03-06 16:52:05.729: E/AndroidRuntime(5419):     at android.app.Activity.onCreateView(Activity.java:4746)
  03-06 16:52:05.729: E/AndroidRuntime(5419):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
  03-06 16:52:05.729: E/AndroidRuntime(5419):     ... 20 more
  </code></pre>