title: Fragments Failed to inflate
body: <p>I'm a little shakey on this so bear with me.  I'm trying to teach myself and I'm currently working out of the WROX Android 4 Application Development for Beginners book. I'm currently on the section "Communication between fragments" and I feel like I'm getting it, but when I run the code taken right out of the book, I get "failed to inflate" in the logcat.  Here's the XMLs followed by the .java files, then the error.  I can't figure out what's killing this:</p>
  
  <p>main.xml:</p>
  
  <pre><code>&lt;LinearLayout 
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:orientation="horizontal"
      &gt;
      &lt;fragment
          android:name="com.example.Fragments.Fragment1"
          android:id="@+id/fragment1"
          android:layout_weight="1"
          android:layout_width="0px"
          android:layout_height="match_parent"
          /&gt;
      &lt;fragment
          android:name="com.example.Fragments.Fragment2"
          android:id="@+id/fragment2"
          android:layout_weight="1"
          android:layout_width="0px"
          android:layout_height="match_parent"
          /&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>fragment1.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:background="#00FF00"
      &gt;
      &lt;TextView 
          android:id="@+id/lblFragment1"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:text="This is fragment #1"
          android:textColor="#000000"
          android:textSize="25sp"
          /&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>fragment2.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:background="#FFFE00"
      &gt;
      &lt;TextView 
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:text="This is fragment #2"
          android:textColor="#000000"
          android:textSize="25sp"
          /&gt;
      &lt;Button
          android:id="@+id/btnGetText"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Get text in Fragment #1"
          android:textColor="#000000"
          android:onClick="onClick"
          /&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>FragmentsActivity.java:</p>
  
  <pre><code>package com.example.fragments;
  
  import android.app.Activity;
  import android.os.Bundle;
  
  public class FragmentsActivity extends Activity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
      }
  }
  </code></pre>
  
  <p>Fragment1.java:</p>
  
  <pre><code>package com.example.fragments;
  
  import android.app.Fragment;
  import android.os.Bundle;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  
  public class Fragment1 extends Fragment {
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          // ---Inflate the layout for this fragment---
          return inflater.inflate(R.layout.fragment1, container, false);
      }
  
  }
  </code></pre>
  
  <p>Fragment2.java:</p>
  
  <pre><code>package com.example.fragments;
  
  import android.app.Fragment;
  import android.os.Bundle;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.Button;
  import android.widget.TextView;
  import android.widget.Toast;
  
  public class Fragment2 extends Fragment {
      @Override
      public View onCreateView(LayoutInflater inflater, 
              ViewGroup container, Bundle savedInstanceState) {
          //---Inflate the layout for this fragment---
          return inflater.inflate(
                  R.layout.fragment2, container, false);
      }
  
      @Override
      public void onStart() {
          super.onStart();
          //---Button View---
          Button btnGetText = (Button)
                  getActivity().findViewById(R.id.btnGetText);
          btnGetText.setOnClickListener(new View.OnClickListener() {
              public void onClick(View v) {
                  TextView lbl = (TextView)
                          getActivity().findViewById(R.id.lblFragment1);
                  Toast.makeText(getActivity(), lbl.getText(), 
                          Toast.LENGTH_SHORT).show();
              }
          });
      }
  }
  </code></pre>
  
  <p>logcat:</p>
  
  <pre><code>04-20 15:09:55.759: E/ActivityThread(9523): Failed to inflate
  04-20 15:09:55.759: E/ActivityThread(9523): android.view.InflateException: Binary XML file line #7: Error inflating class fragment
  04-20 15:09:55.759: E/ActivityThread(9523):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
  04-20 15:09:55.759: E/ActivityThread(9523):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
  04-20 15:09:55.759: E/ActivityThread(9523):     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
  04-20 15:09:55.759: E/ActivityThread(9523):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
  04-20 15:09:55.759: E/ActivityThread(9523):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
  04-20 15:09:55.759: E/ActivityThread(9523):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:282)
  04-20 15:09:55.759: E/ActivityThread(9523):     at android.app.Activity.setContentView(Activity.java:1881)
  04-20 15:09:55.759: E/ActivityThread(9523):     at com.example.fragments.FragmentsActivity.onCreate(FragmentsActivity.java:15)
  04-20 15:09:55.759: E/ActivityThread(9523):     at android.app.Activity.performCreate(Activity.java:5104)
  04-20 15:09:55.759: E/ActivityThread(9523):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
  04-20 15:09:55.759: E/ActivityThread(9523):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2260)
  04-20 15:09:55.759: E/ActivityThread(9523):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2356)
  04-20 15:09:55.759: E/ActivityThread(9523):     at android.app.ActivityThread.access$600(ActivityThread.java:150)
  04-20 15:09:55.759: E/ActivityThread(9523):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
  04-20 15:09:55.759: E/ActivityThread(9523):     at android.os.Handler.dispatchMessage(Handler.java:99)
  04-20 15:09:55.759: E/ActivityThread(9523):     at android.os.Looper.loop(Looper.java:137)
  04-20 15:09:55.759: E/ActivityThread(9523):     at android.app.ActivityThread.main(ActivityThread.java:5195)
  04-20 15:09:55.759: E/ActivityThread(9523):     at java.lang.reflect.Method.invokeNative(Native Method)
  04-20 15:09:55.759: E/ActivityThread(9523):     at java.lang.reflect.Method.invoke(Method.java:511)
  04-20 15:09:55.759: E/ActivityThread(9523):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
  04-20 15:09:55.759: E/ActivityThread(9523):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
  04-20 15:09:55.759: E/ActivityThread(9523):     at dalvik.system.NativeStart.main(Native Method)
  04-20 15:09:55.759: E/ActivityThread(9523): Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.example.Fragments.Fragment1: make sure class name exists, is public, and has an empty constructor that is public
  04-20 15:09:55.759: E/ActivityThread(9523):     at android.app.Fragment.instantiate(Fragment.java:592)
  04-20 15:09:55.759: E/ActivityThread(9523):     at android.app.Fragment.instantiate(Fragment.java:560)
  04-20 15:09:55.759: E/ActivityThread(9523):     at android.app.Activity.onCreateView(Activity.java:4709)
  04-20 15:09:55.759: E/ActivityThread(9523):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
  04-20 15:09:55.759: E/ActivityThread(9523):     ... 21 more
  04-20 15:09:55.759: E/ActivityThread(9523): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.Fragments.Fragment1" on path: /data/app/com.example.fragments-2.apk
  04-20 15:09:55.759: E/ActivityThread(9523):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:65)
  04-20 15:09:55.759: E/ActivityThread(9523):     at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
  04-20 15:09:55.759: E/ActivityThread(9523):     at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
  04-20 15:09:55.759: E/ActivityThread(9523):     at android.app.Fragment.instantiate(Fragment.java:582)
  04-20 15:09:55.759: E/ActivityThread(9523):     ... 24 more
  </code></pre>