title: Android findFragmentById returns null
body: <p>I have an activity that manages a list of fragments. One of these is a ListFragment, and when the activity tries to find it during Create() I get a Null exception.</p>
  
  <pre><code>Caused by: java.lang.NullPointerException
  at com.example.foodexp01b.MainActivity.onCreate(MainActivity.java:154)
  </code></pre>
  
  <p>The ListFragment is not yet created, but I need to have it into the fragment array as the others. What should I do?</p>
  
  <p>Activity</p>
  
  <pre><code>public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
  
          uiHelper = new UiLifecycleHelper(this, callback);
          uiHelper.onCreate(savedInstanceState);
  
          FragmentManager fm = getSupportFragmentManager();
          fragments[LOGIN] = fm.findFragmentById(R.id.loginFragment);
          fragments[HOME] = fm.findFragmentById(R.id.homeFragment);
          fragments[SETTINGS] = fm.findFragmentById(R.id.settingsFragment);
          fragments[SETTINGS].getView().findViewById(R.id.back_button1)
          .setOnClickListener(new OnClickListener() {
              @Override
              public void onClick(View arg0) {
                  currentFragment = HOME;
                  showFragment(HOME, false);
              }
          });
          fragments[DESTINATIONS] = fm.findFragmentById(R.id.destinationFragment);
          fragments[DESTINATIONS].getView().findViewById(R.id.back_button2)
          .setOnClickListener(new OnClickListener() {
              @Override
              public void onClick(View arg0) {
                  currentFragment = HOME;
                  showFragment(HOME, false);
              }
          });
          fragments[MAP] = fm.findFragmentById(R.id.mapFragment);
          fragments[RESTAURANT] = fm.findFragmentById(R.id.restaurantFragment);
          fragments[RESTAURANT].getView().findViewById(R.id.restaurant_back_button)
          .setOnClickListener(new OnClickListener() {
              @Override
              public void onClick(View arg0) {
                  currentFragment = MAP;
                  showFragment(MAP, false);
              }
          });
          fragments[FAVORITES].getView().findViewById(R.id.favoritesFragment);
          FragmentTransaction transaction = fm.beginTransaction();
          for(int i = 0; i &lt; fragments.length; i++) {
              transaction.hide(fragments[i]);
          }
          transaction.commit();
      }
  </code></pre>
  
  <p>the ListFragment code</p>
  
  <pre><code>public class FavoritesFragment extends ListFragment{
  
      private ListView listView;
      private List&lt;BaseListElement&gt; listElements;
  
      OnFavoritesFragmentListener mCallback;
  
      ArrayList&lt;String&gt; favorites;
  
      // Container Activity must implement this interface
      public interface OnFavoritesFragmentListener {
          public void onSearchSelected();
      }
  
      @Override
      public void onAttach(Activity activity) {
          super.onAttach(activity);
  
          // This makes sure that the container activity has implemented
          // the callback interface. If not, it throws an exception
          try {
              mCallback = (OnFavoritesFragmentListener) activity;
          } catch (ClassCastException e) {
              throw new ClassCastException(activity.toString()
                      + " must implement OnFavoritesFragmentListener");
          }
      }
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
      }
  
  
      @Override
      public void onActivityCreated(Bundle savedInstanceState){
          favorites = new ArrayList&lt;String&gt;();
          setListAdapter(new ArrayAdapter&lt;String&gt;(getActivity(),android.R.layout.simple_list_item_1, favorites));
      }
      @Override
      public void onListItemClick(ListView l, View v, int position, long id) {
          //do something
      }
  }
  </code></pre>
  
  <p>EDIT</p>
  
  <p>The activity 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"&gt;
  
      &lt;fragment android:name="com.example.foodexp01b.HomeFragment"
            android:id="@+id/homeFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent" /&gt;
      &lt;fragment android:name="com.example.foodexp01b.LoginFragment"
            android:id="@+id/loginFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent" /&gt;
      &lt;fragment android:name="com.example.foodexp01b.SettingsFragment"
            android:id="@+id/settingsFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent" /&gt;
  
      &lt;fragment android:name="com.example.foodexp01b.DestinationFragment"
            android:id="@+id/destinationFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent" /&gt;
      &lt;fragment
            android:id="@+id/mapFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:name="com.google.android.gms.maps.SupportMapFragment"/&gt;
  
      &lt;fragment android:name="com.example.foodexp01b.RestaurantFragment"
            android:id="@+id/restaurantFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent" /&gt;
  
      &lt;fragment android:name="com.example.foodexp01b.FavoritesFragment"
            android:id="@+id/favoritesFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent" /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>