Merge "cherry-pick af4786bcb5945f72e8639d74611cd5f9b0236710 to froyo" into froyo
diff --git a/core/java/android/widget/GridView.java b/core/java/android/widget/GridView.java
index be633be..d867980 100644
--- a/core/java/android/widget/GridView.java
+++ b/core/java/android/widget/GridView.java
@@ -1128,11 +1128,13 @@
             default:
                 if (childCount == 0) {
                     if (!mStackFromBottom) {
-                        setSelectedPositionInt(0);
+                        setSelectedPositionInt(mAdapter == null || isInTouchMode() ?
+                                INVALID_POSITION : 0);
                         sel = fillFromTop(childrenTop);
                     } else {
                         final int last = mItemCount - 1;
-                        setSelectedPositionInt(last);
+                        setSelectedPositionInt(mAdapter == null || isInTouchMode() ?
+                                INVALID_POSITION : last);
                         sel = fillFromBottom(last, childrenBottom);
                     }
                 } else {