From 1ea6889136cdad8fc01de0665b3a16a4dcb259fe Mon Sep 17 00:00:00 2001 From: Mark Brophy Date: Mon, 1 Aug 2011 16:24:44 +0100 Subject: [PATCH] Fix exception when a focused item is detached. When a ListView has itemsCanFocus set, and scrolling moves the currently focused item off the display, its focus is cleared. This is checked by calling getDistanceToView(). However, it's possible that the view will have been recycled. If so, it will have been detached from the parent by calling ViewGroup.detachViewFromParent. Since this doesn't clear the view's focus, we'll still try to call getDistanceFromView(), causing an IllegalArgumentException since the view is not a descendant of the ListView anymore. Check whether the view is still a descendant before calling getDistanceToView(). If it's not, we also need to clear the focus. Bug: 4556022 Change-Id: Iebee56032223b70d714e2ec3bb7a19093ab5f81c --- core/java/android/widget/ListView.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/java/android/widget/ListView.java b/core/java/android/widget/ListView.java index 1f29b1614006f..946f009ce5ad8 100644 --- a/core/java/android/widget/ListView.java +++ b/core/java/android/widget/ListView.java @@ -2428,7 +2428,7 @@ public class ListView extends AbsListView { if (mItemsCanFocus && (focusResult == null) && selectedView != null && selectedView.hasFocus()) { final View focused = selectedView.findFocus(); - if (distanceToView(focused) > 0) { + if (!isViewAncestorOf(focused, this) || distanceToView(focused) > 0) { focused.clearFocus(); } }