Merge "Fixing AdapterViewAnimator crash when associated adapter is empty"
diff --git a/core/java/android/widget/AdapterViewAnimator.java b/core/java/android/widget/AdapterViewAnimator.java
index 1d1e601..b7b1a23 100644
--- a/core/java/android/widget/AdapterViewAnimator.java
+++ b/core/java/android/widget/AdapterViewAnimator.java
@@ -324,7 +324,11 @@
     }
 
     private int modulo(int pos, int size) {
-        return (size + (pos % size)) % size;
+        if (size > 0) {
+            return (size + (pos % size)) % size;
+        } else {
+            return 0;
+        }
     }
 
     /**
@@ -383,6 +387,8 @@
 
     void showOnly(int childIndex, boolean animate, boolean onLayout) {
         if (mAdapter == null) return;
+        final int adapterCount = mAdapter.getCount();
+        if (adapterCount == 0) return;
 
         for (int i = 0; i < mPreviousViews.size(); i++) {
             View viewToRemove = mViewsMap.get(mPreviousViews.get(i)).view;
@@ -399,7 +405,6 @@
             removeViewInLayout(viewToRemove);
         }
         mPreviousViews.clear();
-        int adapterCount = mAdapter.getCount();
         int newWindowStartUnbounded = childIndex - mActiveOffset;
         int newWindowEndUnbounded = newWindowStartUnbounded + mNumActiveViews - 1;
         int newWindowStart = Math.max(0, newWindowStartUnbounded);
diff --git a/core/java/android/widget/StackView.java b/core/java/android/widget/StackView.java
index 839de7d..f0954e2 100644
--- a/core/java/android/widget/StackView.java
+++ b/core/java/android/widget/StackView.java
@@ -749,7 +749,9 @@
         if (mAdapter != null && mWhichChild == -1) {
             mWhichChild = mAdapter.getCount() - 1;
         }
-        setDisplayedChild(mWhichChild);
+        if (mWhichChild >= 0) {
+            setDisplayedChild(mWhichChild);
+        }
     }
 
     LayoutParams createOrReuseLayoutParams(View v) {