Handle null result from getVirtualChildAt()

Also adds final where the method was being called, adds Nullable
annotation to method, and updates docs.

Bug: 25985497
Change-Id: I847a8507f2e3970f1340cddf4abf8650dda22b35
(cherry picked from commit ad52693cf3dfe3f5e84f5a22fe4c3787f2f2cbfb)
diff --git a/core/java/android/widget/LinearLayout.java b/core/java/android/widget/LinearLayout.java
index b5e08ca..df3d850 100644
--- a/core/java/android/widget/LinearLayout.java
+++ b/core/java/android/widget/LinearLayout.java
@@ -348,7 +348,6 @@
         final int count = getVirtualChildCount();
         for (int i = 0; i < count; i++) {
             final View child = getVirtualChildAt(i);
-
             if (child != null && child.getVisibility() != GONE) {
                 if (hasDividerBeforeChildAt(i)) {
                     final LayoutParams lp = (LayoutParams) child.getLayoutParams();
@@ -377,7 +376,7 @@
      */
     private View getLastNonGoneChild() {
         for (int i = getVirtualChildCount() - 1; i >= 0; i--) {
-            View child = getVirtualChildAt(i);
+            final View child = getVirtualChildAt(i);
             if (child != null && child.getVisibility() != GONE) {
                 return child;
             }
@@ -390,7 +389,6 @@
         final boolean isLayoutRtl = isLayoutRtl();
         for (int i = 0; i < count; i++) {
             final View child = getVirtualChildAt(i);
-
             if (child != null && child.getVisibility() != GONE) {
                 if (hasDividerBeforeChildAt(i)) {
                     final LayoutParams lp = (LayoutParams) child.getLayoutParams();
@@ -577,8 +575,9 @@
      * for an example.</p>
      *
      * @param index the child's index
-     * @return the child at the specified index
+     * @return the child at the specified index, may be {@code null}
      */
+    @Nullable
     View getVirtualChildAt(int index) {
         return getChildAt(index);
     }
@@ -659,7 +658,7 @@
      */
     private boolean allViewsAreGoneBefore(int childIndex) {
         for (int i = childIndex - 1; i >= 0; i--) {
-            View child = getVirtualChildAt(i);
+            final View child = getVirtualChildAt(i);
             if (child != null && child.getVisibility() != GONE) {
                 return false;
             }
@@ -703,7 +702,6 @@
         // See how tall everyone is. Also remember max width.
         for (int i = 0; i < count; ++i) {
             final View child = getVirtualChildAt(i);
-
             if (child == null) {
                 mTotalLength += measureNullChild(i);
                 continue;
@@ -822,7 +820,6 @@
 
             for (int i = 0; i < count; ++i) {
                 final View child = getVirtualChildAt(i);
-
                 if (child == null) {
                     mTotalLength += measureNullChild(i);
                     continue;
@@ -938,7 +935,6 @@
             if (useLargestChild && heightMode != MeasureSpec.EXACTLY) {
                 for (int i = 0; i < count; i++) {
                     final View child = getVirtualChildAt(i);
-
                     if (child == null || child.getVisibility() == View.GONE) {
                         continue;
                     }
@@ -981,7 +977,7 @@
                 MeasureSpec.EXACTLY);
         for (int i = 0; i< count; ++i) {
            final View child = getVirtualChildAt(i);
-           if (child.getVisibility() != GONE) { 
+           if (child != null && child.getVisibility() != GONE) {
                LinearLayout.LayoutParams lp = ((LinearLayout.LayoutParams)child.getLayoutParams());
                
                if (lp.width == LayoutParams.MATCH_PARENT) {
@@ -1047,7 +1043,6 @@
         // See how wide everyone is. Also remember max height.
         for (int i = 0; i < count; ++i) {
             final View child = getVirtualChildAt(i);
-
             if (child == null) {
                 mTotalLength += measureNullChild(i);
                 continue;
@@ -1203,7 +1198,6 @@
 
             for (int i = 0; i < count; ++i) {
                 final View child = getVirtualChildAt(i);
-
                 if (child == null) {
                     mTotalLength += measureNullChild(i);
                     continue;
@@ -1361,7 +1355,6 @@
             if (useLargestChild && widthMode != MeasureSpec.EXACTLY) {
                 for (int i = 0; i < count; i++) {
                     final View child = getVirtualChildAt(i);
-
                     if (child == null || child.getVisibility() == View.GONE) {
                         continue;
                     }
@@ -1406,7 +1399,7 @@
                 MeasureSpec.EXACTLY);
         for (int i = 0; i < count; ++i) {
            final View child = getVirtualChildAt(i);
-           if (child.getVisibility() != GONE) { 
+           if (child != null && child.getVisibility() != GONE) {
                LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) child.getLayoutParams();
                
                if (lp.height == LayoutParams.MATCH_PARENT) {
@@ -1666,9 +1659,8 @@
         }
 
         for (int i = 0; i < count; i++) {
-            int childIndex = start + dir * i;
+            final int childIndex = start + dir * i;
             final View child = getVirtualChildAt(childIndex);
-
             if (child == null) {
                 childLeft += measureNullChild(childIndex);
             } else if (child.getVisibility() != GONE) {
diff --git a/core/java/android/widget/TableRow.java b/core/java/android/widget/TableRow.java
index f7f9c91..22931fc 100644
--- a/core/java/android/widget/TableRow.java
+++ b/core/java/android/widget/TableRow.java
@@ -98,7 +98,7 @@
      * {@hide}
      */
     void setColumnCollapsed(int columnIndex, boolean collapsed) {
-        View child = getVirtualChildAt(columnIndex);
+        final View child = getVirtualChildAt(columnIndex);
         if (child != null) {
             child.setVisibility(collapsed ? GONE : VISIBLE);
         }