From 898c7045e08bdb2209efbccf853a3376fc2070ff Mon Sep 17 00:00:00 2001 From: Alan Viverette Date: Wed, 26 Aug 2015 15:21:39 -0400 Subject: [PATCH] Avoid NPE when getPositionForView() is called on detached view Bug: 23557674 Change-Id: I76cb5f06081b2ac4c8f535df8d1a0aee17d2bb20 --- core/java/android/widget/AdapterView.java | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/core/java/android/widget/AdapterView.java b/core/java/android/widget/AdapterView.java index 696271105639d..0cc1b25d19b77 100644 --- a/core/java/android/widget/AdapterView.java +++ b/core/java/android/widget/AdapterView.java @@ -612,7 +612,7 @@ public abstract class AdapterView extends ViewGroup { View listItem = view; try { View v; - while (!(v = (View) listItem.getParent()).equals(this)) { + while ((v = (View) listItem.getParent()) != null && !v.equals(this)) { listItem = v; } } catch (ClassCastException e) { @@ -620,11 +620,13 @@ public abstract class AdapterView extends ViewGroup { return INVALID_POSITION; } - // Search the children for the list item - final int childCount = getChildCount(); - for (int i = 0; i < childCount; i++) { - if (getChildAt(i).equals(listItem)) { - return mFirstPosition + i; + if (listItem != null) { + // Search the children for the list item + final int childCount = getChildCount(); + for (int i = 0; i < childCount; i++) { + if (getChildAt(i).equals(listItem)) { + return mFirstPosition + i; + } } }