OSDN Git Service

Protect FragmentManager against View.NO_ID container ids
authorAdam Powell <adamp@google.com>
Tue, 23 Feb 2016 23:25:42 +0000 (15:25 -0800)
committerAdam Powell <adamp@google.com>
Tue, 23 Feb 2016 23:25:42 +0000 (15:25 -0800)
Not all code paths for FragmentManager were checking that the
container view had a valid view id. As we can't correctly restore a
fragment with a container without one, throw a more descriptive
exception earlier.

Bug 27290033

Change-Id: I86e41d2f9b5197e058a7ce154c682cbcc2f9c6eb

core/java/android/app/BackStackRecord.java
core/java/android/app/FragmentManager.java

index eb4b13e..4b0dfc7 100644 (file)
@@ -470,6 +470,10 @@ final class BackStackRecord extends FragmentTransaction implements
         }
 
         if (containerViewId != 0) {
+            if (containerViewId == View.NO_ID) {
+                throw new IllegalArgumentException("Can't add fragment "
+                        + fragment + " with tag " + tag + " to container view with no id");
+            }
             if (fragment.mFragmentId != 0 && fragment.mFragmentId != containerViewId) {
                 throw new IllegalStateException("Can't change container ID of fragment "
                         + fragment + ": was " + fragment.mFragmentId
index 4c8761c..78a054b 100644 (file)
@@ -24,6 +24,7 @@ import android.animation.PropertyValuesHolder;
 import android.animation.ValueAnimator;
 import android.content.Context;
 import android.content.res.Configuration;
+import android.content.res.Resources.NotFoundException;
 import android.content.res.TypedArray;
 import android.os.Bundle;
 import android.os.Debug;
@@ -960,12 +961,24 @@ final class FragmentManagerImpl extends FragmentManager implements LayoutInflate
                         if (!f.mFromLayout) {
                             ViewGroup container = null;
                             if (f.mContainerId != 0) {
-                                container = (ViewGroup)mContainer.onFindViewById(f.mContainerId);
+                                if (f.mContainerId == View.NO_ID) {
+                                    throwException(new IllegalArgumentException(
+                                            "Cannot create fragment "
+                                                    + f
+                                                    + " for a container view with no id"));
+                                }
+                                container = (ViewGroup) mContainer.onFindViewById(f.mContainerId);
                                 if (container == null && !f.mRestored) {
+                                    String resName;
+                                    try {
+                                        resName = f.getResources().getResourceName(f.mContainerId);
+                                    } catch (NotFoundException e) {
+                                        resName = "unknown";
+                                    }
                                     throwException(new IllegalArgumentException(
                                             "No view found for id 0x"
                                             + Integer.toHexString(f.mContainerId) + " ("
-                                            + f.getResources().getResourceName(f.mContainerId)
+                                            + resName
                                             + ") for fragment " + f));
                                 }
                             }