diff options
| author | 2013-06-18 14:42:58 -0700 | |
|---|---|---|
| committer | 2013-06-18 15:49:16 -0700 | |
| commit | f2fb76cc23e18f7b15f7244e6352d024b5008f38 (patch) | |
| tree | 45a24b2b939d4e27d9bfe6ac6cc3609bb221246b | |
| parent | fafe88c3d983f1578e096af69ab8420c846b237e (diff) | |
Fix potential NPE if there is no child in HorizontalScrollView
- related to the previous change for saving the scroll position
- also related to bug #9463581 NPE observed while launching News&Weather app from all apps tray
Change-Id: I9f2f8a246e793eefa1cf510e15a56a1058fb9c18
| -rw-r--r-- | core/java/android/widget/HorizontalScrollView.java | 18 |
1 files changed, 13 insertions, 5 deletions
diff --git a/core/java/android/widget/HorizontalScrollView.java b/core/java/android/widget/HorizontalScrollView.java index c89c91ee82fa..74225a070e4b 100644 --- a/core/java/android/widget/HorizontalScrollView.java +++ b/core/java/android/widget/HorizontalScrollView.java @@ -1456,14 +1456,22 @@ public class HorizontalScrollView extends FrameLayout { @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { - // There is only one child - final View child = getChildAt(0); - final int childWidth = child.getMeasuredWidth(); - final LayoutParams childParams = (LayoutParams) child.getLayoutParams(); + int childWidth = 0; + int childMargins = 0; + + if (getChildCount() > 0) { + childWidth = getChildAt(0).getMeasuredWidth(); + LayoutParams childParams = (LayoutParams) getChildAt(0).getLayoutParams(); + childMargins = childParams.leftMargin + childParams.rightMargin; + } + final int available = r - l - getPaddingLeftWithForeground() - - getPaddingRightWithForeground() - childParams.leftMargin - childParams.rightMargin; + getPaddingRightWithForeground() - childMargins; + final boolean forceLeftGravity = (childWidth > available); + layoutChildren(l, t, r, b, forceLeftGravity); + mIsLayoutDirty = false; // Give a child focus if it needs it if (mChildToScrollTo != null && isViewDescendantOf(mChildToScrollTo, this)) { |