summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Adam Powell <adamp@google.com> 2014-10-09 09:44:18 -0700
committer Adam Powell <adamp@google.com> 2014-10-09 09:47:03 -0700
commit45a9da5076f4482e82ada5f394a9e69f730b9734 (patch)
treeab465e0b22d3bafde561a0b1103d6eac7d4391f7
parente6411c76c44930cd893b78ea9f59f26a4a441881 (diff)
Throw a more descriptive exception when adding null to a ViewGroup
Help developers during debugging bad calls to addView by throwing an IllegalArgumentException earlier rather than implicitly tossing NPE the first time the child view is accessed. Change-Id: I4a554635815a5d0b415f637b93592fb45973f26d
-rw-r--r--core/java/android/view/ViewGroup.java10
1 files changed, 10 insertions, 0 deletions
diff --git a/core/java/android/view/ViewGroup.java b/core/java/android/view/ViewGroup.java
index 3a183ed6707b..2921cfaa03b4 100644
--- a/core/java/android/view/ViewGroup.java
+++ b/core/java/android/view/ViewGroup.java
@@ -3671,6 +3671,9 @@ public abstract class ViewGroup extends View implements ViewParent, ViewManager
* @see #generateDefaultLayoutParams()
*/
public void addView(View child, int index) {
+ if (child == null) {
+ throw new IllegalArgumentException("Cannot add a null child view to a ViewGroup");
+ }
LayoutParams params = child.getLayoutParams();
if (params == null) {
params = generateDefaultLayoutParams();
@@ -3728,6 +3731,10 @@ public abstract class ViewGroup extends View implements ViewParent, ViewManager
System.out.println(this + " addView");
}
+ if (child == null) {
+ throw new IllegalArgumentException("Cannot add a null child view to a ViewGroup");
+ }
+
// addViewInner() will call child.requestLayout() when setting the new LayoutParams
// therefore, we call requestLayout() on ourselves before, so that the child's request
// will be blocked at our level
@@ -3855,6 +3862,9 @@ public abstract class ViewGroup extends View implements ViewParent, ViewManager
*/
protected boolean addViewInLayout(View child, int index, LayoutParams params,
boolean preventRequestLayout) {
+ if (child == null) {
+ throw new IllegalArgumentException("Cannot add a null child view to a ViewGroup");
+ }
child.mParent = null;
addViewInner(child, index, params, preventRequestLayout);
child.mPrivateFlags = (child.mPrivateFlags & ~PFLAG_DIRTY_MASK) | PFLAG_DRAWN;