diff --git a/core/java/android/app/BackStackRecord.java b/core/java/android/app/BackStackRecord.java index eb4b13e69e9de..4b0dfc7e0a8b8 100644 --- a/core/java/android/app/BackStackRecord.java +++ b/core/java/android/app/BackStackRecord.java @@ -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 diff --git a/core/java/android/app/FragmentManager.java b/core/java/android/app/FragmentManager.java index 4c8761c86b930..78a054b3a717c 100644 --- a/core/java/android/app/FragmentManager.java +++ b/core/java/android/app/FragmentManager.java @@ -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)); } }