title: ClassCastException: Fragment cannot be cast to ListFragment
body: <p>I'm new to fragments and working off an example from "Professional Android 4 Application Development" Chapter 4, I think. I tried the code straight from the book that doesn't use the support library and the support library but I still get the same ClassCastException.</p>
  
  <p><strong>ToDoListActivity.java</strong></p>
  
  <pre><code>package com.example.todolist4;
  
  import java.util.ArrayList;
  
  import android.support.v4.app.FragmentManager;
  import android.os.Bundle;
  import android.support.v4.app.FragmentActivity;
  import android.widget.ArrayAdapter;
  
  public class ToDoListActivity extends FragmentActivity implements NewItemFragment.OnNewItemAddedListener {
  
  private ArrayAdapter&lt;String&gt; aa;
  private ArrayList&lt;String&gt; todoItems;
  
  public void onCreate(Bundle savedInstanceState){
      super.onCreate(savedInstanceState);
  
      //Inflate you view
      setContentView(R.layout.main);
  
      // Get references to the Fragments
      FragmentManager fm = getSupportFragmentManager();
      ToDoListFragment todoListFragment = (ToDoListFragment)    fm.findFragmentById(R.id.TodoListFragment);
  
      // Create the array list of todo items
      todoItems = new ArrayList&lt;String&gt;();
  
      // Create the array adapter to bind the array to the listview
      aa = new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_list_item_1, todoItems);
  
      // Bind the array adapter to the listview.
      todoListFragment.setListAdapter(aa);
  }
  
  public void onNewItemAdded(String newItem){
      todoItems.add(newItem);
      aa.notifyDataSetChanged();
  }
  </code></pre>
  
  <p>}</p>
  
  <p><strong>NewItemFragment.java</strong></p>
  
  <pre><code>package com.example.todolist4;
  
  import android.app.Activity;
  import android.support.v4.app.Fragment;
  import android.view.KeyEvent;
  import android.os.Bundle;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.EditText;
  
  
  public class NewItemFragment extends Fragment {
  
  private OnNewItemAddedListener onNewItemAddedListener;
  
  public interface OnNewItemAddedListener {
      public void onNewItemAdded(String newItem);
  }
  
  @Override
  public View onCreateView(LayoutInflater inflater,
                           ViewGroup container,
                           Bundle savedInstanceState) {
  
      View view = inflater.inflate(R.layout.new_item_fragment, container, false);
  
      final EditText myEditText = (EditText)view.findViewById(R.id.myEditText);
  
      myEditText.setOnKeyListener(new View.OnKeyListener(){
          public boolean onKey(View v, int keyCode, KeyEvent event) {
              if( event.getAction() == KeyEvent.ACTION_DOWN)
                  if( ( keyCode == KeyEvent.KEYCODE_DPAD_CENTER) ||
                      ( keyCode == KeyEvent.KEYCODE_ENTER)){
                      String newItem = myEditText.getText().toString();
                      myEditText.setText("");
                      return true;
                  }
              return false;
          }
      });
  
      return view;
  }
  
  @Override
  public void onAttach(Activity activity) {
      super.onAttach(activity);
  
      try{
          onNewItemAddedListener = (OnNewItemAddedListener)activity;
      } catch (ClassCastException e) {
          throw new ClassCastException(activity.toString() + " must implement  OnNewItemAddedListener" );
          }
      }
  }
  </code></pre>
  
  <p><strong>ToDoListFragment.java</strong></p>
  
  <pre><code>package com.example.todolist4;
  
  import android.support.v4.app.ListFragment;
  
  public class ToDoListFragment extends ListFragment {
  
  }
  </code></pre>
  
  <p><strong>main.xml</strong></p>
  
  <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="match_parent"
      android:layout_height="match_parent"&gt;
      &lt;fragment android:name="com.example.todolist4.NewItemFragment"
          android:id="@+id/NewItemFragment"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          /&gt;
      &lt;fragment android:name="com.example.todolist4.NewItemFragment"
          android:id="@+id/TodoListFragment"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          /&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p><strong>new_item_fragment.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  
  &lt;EditText xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/myEditText"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:hint="@string/addItemHint"
      android:contentDescription="@string/addItemContentDescription"/&gt;
  </code></pre>
  
  <p><strong>Logcat</strong></p>
  
  <pre><code>Caused by: java.lang.ClassCastException: com.example.todolist4.NewItemFragment cannot be cast to com.example.todolist4.ToDoListFragment
              at com.example.todolist4.ToDoListActivity.onCreate(ToDoListActivity.java:23)
              at android.app.Activity.performCreate(Activity.java:5104)
              at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
              at android.app.ActivityThread.access$600(ActivityThread.java:141)
              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
              at android.os.Handler.dispatchMessage(Handler.java:99)
              at android.os.Looper.loop(Looper.java:137)
              at android.app.ActivityThread.main(ActivityThread.java:5041)
              at java.lang.reflect.Method.invokeNative(Native Method)
              at java.lang.reflect.Method.invoke(Method.java:511)
              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
              at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>the exception is being thrown on this line</p>
  
  <pre><code>ToDoListFragment todoListFragment = (ToDoListFragment) fm.findFragmentById(R.id.TodoListFragment);
  </code></pre>
  
  <p>Any suggestions? </p>