Retry: Set up fragments just after setContentView().

Redo of I9e0ad079 with fix for crash on portrait.

I9e0ad079 crashed if you launched the app on portrait, because contact details
fragments don't exist in the layout.
(It worked when you rotated from landscape to portrait, because the fragment
manager would recrate them even though the layout didin't have them.)

The only change from I9e0ad079 is that we still use onAttachFragment to
initialize details fragments.

* Original CL description:

... except for ContactsUnavailableFragment, which is not in the layout.
(we dynamically create it.)

It's part of refactoring to prepare for ViewPager.

This also fixes the "mFavoritesFragment and mFrequentFragment are both
StrequentContactListFragment but we always assume StrequentContactListFragment
is Favorites in onAttachFragment" issue.

Change-Id: If30611039d8cdaa8f91676454eba67e89fcbdcc8
diff --git a/src/com/android/contacts/ContactsActivity.java b/src/com/android/contacts/ContactsActivity.java
index b78fad2..1414f80 100644
--- a/src/com/android/contacts/ContactsActivity.java
+++ b/src/com/android/contacts/ContactsActivity.java
@@ -19,10 +19,13 @@
 import com.android.contacts.test.InjectedServices;
 
 import android.app.Activity;
+import android.app.Fragment;
+import android.app.FragmentManager;
 import android.content.ContentResolver;
 import android.content.Intent;
 import android.content.SharedPreferences;
 import android.os.Bundle;
+import android.view.View;
 
 /**
  * A common superclass for Contacts activities that handles application-wide services.
@@ -86,4 +89,32 @@
     public void onServiceCompleted(Intent callbackIntent) {
         onNewIntent(callbackIntent);
     }
+
+    /**
+     * Convenient version of {@link FragmentManager#findFragmentById(int)}, which throws
+     * an exception if the fragment doesn't exist.
+     */
+    @SuppressWarnings("unchecked")
+    public <T extends Fragment> T getFragment(int id) {
+        T result = (T)getFragmentManager().findFragmentById(id);
+        if (result == null) {
+            throw new IllegalArgumentException("fragment 0x" + Integer.toHexString(id)
+                    + " doesn't exist");
+        }
+        return result;
+    }
+
+    /**
+     * Convenient version of {@link #findViewById(int)}, which throws
+     * an exception if the view doesn't exist.
+     */
+    @SuppressWarnings("unchecked")
+    public <T extends View> T getView(int id) {
+        T result = (T)findViewById(id);
+        if (result == null) {
+            throw new IllegalArgumentException("view 0x" + Integer.toHexString(id)
+                    + " doesn't exist");
+        }
+        return result;
+    }
 }