Fixes #1972421. Prevents crash in ScrollView/HorizontalScrollView.

Add several checks to make sure there's at least one child.
diff --git a/core/java/android/gesture/Gesture.java b/core/java/android/gesture/Gesture.java
index c92f665..62330e1 100755
--- a/core/java/android/gesture/Gesture.java
+++ b/core/java/android/gesture/Gesture.java
@@ -31,6 +31,7 @@
 import java.io.ByteArrayOutputStream;
 import java.io.ByteArrayInputStream;
 import java.util.ArrayList;
+import java.util.concurrent.atomic.AtomicInteger;
 
 /**
  * A gesture can have a single or multiple strokes
@@ -44,7 +45,7 @@
     private static final boolean BITMAP_RENDERING_ANTIALIAS = true;
     private static final boolean BITMAP_RENDERING_DITHER = true;
 
-    private static int sGestureCount = 0;
+    private static final AtomicInteger sGestureCount = new AtomicInteger(0);
 
     private final RectF mBoundingBox = new RectF();
 
@@ -54,7 +55,7 @@
     private final ArrayList<GestureStroke> mStrokes = new ArrayList<GestureStroke>();
 
     public Gesture() {
-        mGestureID = GESTURE_ID_BASE + sGestureCount++;
+        mGestureID = GESTURE_ID_BASE + sGestureCount.incrementAndGet();
     }
 
     /**
diff --git a/core/java/android/widget/HorizontalScrollView.java b/core/java/android/widget/HorizontalScrollView.java
index f86b37c..46e514c 100644
--- a/core/java/android/widget/HorizontalScrollView.java
+++ b/core/java/android/widget/HorizontalScrollView.java
@@ -286,18 +286,20 @@
             return;
         }
 
-        final View child = getChildAt(0);
-        int width = getMeasuredWidth();
-        if (child.getMeasuredWidth() < width) {
-            final FrameLayout.LayoutParams lp = (LayoutParams) child.getLayoutParams();
-
-            int childHeightMeasureSpec = getChildMeasureSpec(heightMeasureSpec, mPaddingTop
-                    + mPaddingBottom, lp.height);
-            width -= mPaddingLeft;
-            width -= mPaddingRight;
-            int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY);
-
-            child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
+        if (getChildCount() > 0) {
+            final View child = getChildAt(0);
+            int width = getMeasuredWidth();
+            if (child.getMeasuredWidth() < width) {
+                final FrameLayout.LayoutParams lp = (LayoutParams) child.getLayoutParams();
+    
+                int childHeightMeasureSpec = getChildMeasureSpec(heightMeasureSpec, mPaddingTop
+                        + mPaddingBottom, lp.height);
+                width -= mPaddingLeft;
+                width -= mPaddingRight;
+                int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY);
+    
+                child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
+            }
         }
     }
 
@@ -636,7 +638,7 @@
             mTempRect.left = getScrollX() + width;
             int count = getChildCount();
             if (count > 0) {
-                View view = getChildAt(count - 1);
+                View view = getChildAt(0);
                 if (mTempRect.left + width > view.getRight()) {
                     mTempRect.left = view.getRight() - width;
                 }
@@ -674,7 +676,7 @@
         if (right) {
             int count = getChildCount();
             if (count > 0) {
-                View view = getChildAt(count - 1);
+                View view = getChildAt(0);
                 mTempRect.right = view.getRight();
                 mTempRect.left = mTempRect.right - width;
             }
@@ -751,9 +753,9 @@
 
             if (direction == View.FOCUS_LEFT && getScrollX() < scrollDelta) {
                 scrollDelta = getScrollX();
-            } else if (direction == View.FOCUS_RIGHT) {
-
-                int daRight = getChildAt(getChildCount() - 1).getRight();
+            } else if (direction == View.FOCUS_RIGHT && getChildCount() > 0) {
+                
+                int daRight = getChildAt(0).getRight();
 
                 int screenRight = getScrollX() + getWidth();
 
@@ -975,6 +977,7 @@
      * @return The scroll delta.
      */
     protected int computeScrollDeltaToGetChildRectOnScreen(Rect rect) {
+        if (getChildCount() == 0) return 0;
 
         int width = getWidth();
         int screenLeft = getScrollX();
@@ -1008,7 +1011,7 @@
             }
 
             // make sure we aren't scrolling beyond the end of our content
-            int right = getChildAt(getChildCount() - 1).getRight();
+            int right = getChildAt(0).getRight();
             int distanceToRight = right - screenRight;
             scrollXDelta = Math.min(scrollXDelta, distanceToRight);
 
@@ -1148,27 +1151,29 @@
      *                  which means we want to scroll towards the left.
      */
     public void fling(int velocityX) {
-        int width = getWidth() - mPaddingRight - mPaddingLeft;
-        int right = getChildAt(0).getWidth();
-
-        mScroller.fling(mScrollX, mScrollY, velocityX, 0, 0, right - width, 0, 0);
-
-        final boolean movingRight = velocityX > 0;
-
-        View newFocused = findFocusableViewInMyBounds(movingRight,
-                mScroller.getFinalX(), findFocus());
-
-        if (newFocused == null) {
-            newFocused = this;
+        if (getChildCount() > 0) {
+            int width = getWidth() - mPaddingRight - mPaddingLeft;
+            int right = getChildAt(0).getWidth();
+    
+            mScroller.fling(mScrollX, mScrollY, velocityX, 0, 0, right - width, 0, 0);
+    
+            final boolean movingRight = velocityX > 0;
+    
+            View newFocused = findFocusableViewInMyBounds(movingRight,
+                    mScroller.getFinalX(), findFocus());
+    
+            if (newFocused == null) {
+                newFocused = this;
+            }
+    
+            if (newFocused != findFocus()
+                    && newFocused.requestFocus(movingRight ? View.FOCUS_RIGHT : View.FOCUS_LEFT)) {
+                mScrollViewMovedFocus = true;
+                mScrollViewMovedFocus = false;
+            }
+    
+            invalidate();
         }
-
-        if (newFocused != findFocus()
-                && newFocused.requestFocus(movingRight ? View.FOCUS_RIGHT : View.FOCUS_LEFT)) {
-            mScrollViewMovedFocus = true;
-            mScrollViewMovedFocus = false;
-        }
-
-        invalidate();
     }
 
     /**
diff --git a/core/java/android/widget/ScrollView.java b/core/java/android/widget/ScrollView.java
index 90e1242..703cd8e2 100644
--- a/core/java/android/widget/ScrollView.java
+++ b/core/java/android/widget/ScrollView.java
@@ -20,8 +20,6 @@
 import android.content.res.TypedArray;
 import android.graphics.Rect;
 import android.util.AttributeSet;
-import android.util.Config;
-import android.util.Log;
 import android.view.FocusFinder;
 import android.view.KeyEvent;
 import android.view.MotionEvent;
@@ -54,7 +52,6 @@
  */
 public class ScrollView extends FrameLayout {
     static final String TAG = "ScrollView";
-    static final boolean localLOGV = false || Config.LOGV;
     
     static final int ANIMATED_SCROLL_GAP = 250;
 
@@ -287,18 +284,21 @@
             return;
         }
 
-        final View child = getChildAt(0);
-        int height = getMeasuredHeight();
-        if (child.getMeasuredHeight() < height) {
-            final FrameLayout.LayoutParams lp = (LayoutParams) child.getLayoutParams();
-
-            int childWidthMeasureSpec = getChildMeasureSpec(widthMeasureSpec, mPaddingLeft
-                    + mPaddingRight, lp.width);
-            height -= mPaddingTop;
-            height -= mPaddingBottom;
-            int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
-
-            child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
+        if (getChildCount() > 0) {
+            final View child = getChildAt(0);
+            int height = getMeasuredHeight();
+            if (child.getMeasuredHeight() < height) {
+                final FrameLayout.LayoutParams lp = (LayoutParams) child.getLayoutParams();
+    
+                int childWidthMeasureSpec = getChildMeasureSpec(widthMeasureSpec, mPaddingLeft
+                        + mPaddingRight, lp.width);
+                height -= mPaddingTop;
+                height -= mPaddingBottom;
+                int childHeightMeasureSpec =
+                        MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
+    
+                child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
+            }
         }
     }
 
@@ -756,13 +756,14 @@
             if (direction == View.FOCUS_UP && getScrollY() < scrollDelta) {
                 scrollDelta = getScrollY();
             } else if (direction == View.FOCUS_DOWN) {
-
-                int daBottom = getChildAt(getChildCount() - 1).getBottom();
-
-                int screenBottom = getScrollY() + getHeight();
-
-                if (daBottom - screenBottom < maxJump) {
-                    scrollDelta = daBottom - screenBottom;
+                if (getChildCount() > 0) {
+                    int daBottom = getChildAt(0).getBottom();
+    
+                    int screenBottom = getScrollY() + getHeight();
+    
+                    if (daBottom - screenBottom < maxJump) {
+                        scrollDelta = daBottom - screenBottom;
+                    }
                 }
             }
             if (scrollDelta == 0) {
@@ -830,16 +831,12 @@
     public final void smoothScrollBy(int dx, int dy) {
         long duration = AnimationUtils.currentAnimationTimeMillis() - mLastScroll;
         if (duration > ANIMATED_SCROLL_GAP) {
-            if (localLOGV) Log.v(TAG, "Smooth scroll: mScrollY=" + mScrollY
-                    + " dy=" + dy);
             mScroller.startScroll(mScrollX, mScrollY, dx, dy);
             invalidate();
         } else {
             if (!mScroller.isFinished()) {
                 mScroller.abortAnimation();
             }
-            if (localLOGV) Log.v(TAG, "Immediate scroll: mScrollY=" + mScrollY
-                    + " dy=" + dy);
             scrollBy(dx, dy);
         }
         mLastScroll = AnimationUtils.currentAnimationTimeMillis();
@@ -922,9 +919,6 @@
                 View child = getChildAt(0);
                 mScrollX = clamp(x, getWidth() - mPaddingRight - mPaddingLeft, child.getWidth());
                 mScrollY = clamp(y, getHeight() - mPaddingBottom - mPaddingTop, child.getHeight());
-                if (localLOGV) Log.v(TAG, "mScrollY=" + mScrollY + " y=" + y
-                        + " height=" + this.getHeight()
-                        + " child height=" + child.getHeight());
             } else {
                 mScrollX = x;
                 mScrollY = y;
@@ -986,6 +980,7 @@
      * @return The scroll delta.
      */
     protected int computeScrollDeltaToGetChildRectOnScreen(Rect rect) {
+        if (getChildCount() == 0) return 0;
 
         int height = getHeight();
         int screenTop = getScrollY();
@@ -1005,9 +1000,6 @@
 
         int scrollYDelta = 0;
 
-        if (localLOGV) Log.v(TAG, "child=" + rect.toShortString()
-                + " screenTop=" + screenTop + " screenBottom=" + screenBottom
-                + " height=" + height);
         if (rect.bottom > screenBottom && rect.top > screenTop) {
             // need to move down to get it in view: move down just enough so
             // that the entire rectangle is in view (or at least the first
@@ -1022,10 +1014,8 @@
             }
 
             // make sure we aren't scrolling beyond the end of our content
-            int bottom = getChildAt(getChildCount() - 1).getBottom();
+            int bottom = getChildAt(0).getBottom();
             int distanceToBottom = bottom - screenBottom;
-            if (localLOGV) Log.v(TAG, "scrollYDelta=" + scrollYDelta
-                    + " distanceToBottom=" + distanceToBottom);
             scrollYDelta = Math.min(scrollYDelta, distanceToBottom);
 
         } else if (rect.top < screenTop && rect.bottom < screenBottom) {
@@ -1164,26 +1154,28 @@
      *                  which means we want to scroll towards the top.
      */
     public void fling(int velocityY) {
-        int height = getHeight() - mPaddingBottom - mPaddingTop;
-        int bottom = getChildAt(0).getHeight();
-
-        mScroller.fling(mScrollX, mScrollY, 0, velocityY, 0, 0, 0, bottom - height);
-
-        final boolean movingDown = velocityY > 0;
-
-        View newFocused =
-                findFocusableViewInMyBounds(movingDown, mScroller.getFinalY(), findFocus());
-        if (newFocused == null) {
-            newFocused = this;
+        if (getChildCount() > 0) {
+            int height = getHeight() - mPaddingBottom - mPaddingTop;
+            int bottom = getChildAt(0).getHeight();
+    
+            mScroller.fling(mScrollX, mScrollY, 0, velocityY, 0, 0, 0, bottom - height);
+    
+            final boolean movingDown = velocityY > 0;
+    
+            View newFocused =
+                    findFocusableViewInMyBounds(movingDown, mScroller.getFinalY(), findFocus());
+            if (newFocused == null) {
+                newFocused = this;
+            }
+    
+            if (newFocused != findFocus()
+                    && newFocused.requestFocus(movingDown ? View.FOCUS_DOWN : View.FOCUS_UP)) {
+                mScrollViewMovedFocus = true;
+                mScrollViewMovedFocus = false;
+            }
+    
+            invalidate();
         }
-
-        if (newFocused != findFocus()
-                && newFocused.requestFocus(movingDown ? View.FOCUS_DOWN : View.FOCUS_UP)) {
-            mScrollViewMovedFocus = true;
-            mScrollViewMovedFocus = false;
-        }
-
-        invalidate();
     }
 
     /**