OSDN Git Service

Fix issue #6595555: deal better with fragmentless headers.
authorDianne Hackborn <hackbod@google.com>
Tue, 12 Feb 2013 19:42:51 +0000 (11:42 -0800)
committerDianne Hackborn <hackbod@google.com>
Tue, 12 Feb 2013 19:42:51 +0000 (11:42 -0800)
If you constructed a preference activity whose first header
didn't have a fragment, this would crash in two-pane mode
because it would try to use that header as the initially
selected header but not have a fragment to show.

We need to have a fragment for whatever header we switch to,
so the code now looks for the first header with a fragment
as the initial header.

Also added some error checks to throw more descriptive
exceptions on bad headers -- when there is no header at all
with a fragment, if the app tries to manually switch to a
header without a fragment.

Change-Id: Ia84221fcb2fe5755bb674e0606ac2a1fcde4cdc9

core/java/android/preference/PreferenceActivity.java

index 09ff7be..028317f 100644 (file)
@@ -703,7 +703,13 @@ public abstract class PreferenceActivity extends ListActivity implements
      * show for the initial UI.
      */
     public Header onGetInitialHeader() {
-        return mHeaders.get(0);
+        for (int i=0; i<mHeaders.size(); i++) {
+            Header h = mHeaders.get(i);
+            if (h.fragment != null) {
+                return h;
+            }
+        }
+        throw new IllegalStateException("Must have at least one header with a fragment");
     }
 
     /**
@@ -1167,6 +1173,9 @@ public abstract class PreferenceActivity extends ListActivity implements
             getFragmentManager().popBackStack(BACK_STACK_PREFS,
                     FragmentManager.POP_BACK_STACK_INCLUSIVE);
         } else {
+            if (header.fragment == null) {
+                throw new IllegalStateException("can't switch to header that has no fragment");
+            }
             int direction = mHeaders.indexOf(header) - mHeaders.indexOf(mCurHeader);
             switchToHeaderInner(header.fragment, header.fragmentArguments, direction);
             setSelectedHeader(header);