OSDN Git Service

Fix potential NPE if there is no child in HorizontalScrollView
authorFabrice Di Meglio <fdimeglio@google.com>
Tue, 18 Jun 2013 21:42:58 +0000 (14:42 -0700)
committerFabrice Di Meglio <fdimeglio@google.com>
Tue, 18 Jun 2013 22:49:16 +0000 (15:49 -0700)
- 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

core/java/android/widget/HorizontalScrollView.java

index c89c91e..74225a0 100644 (file)
@@ -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)) {