From 827bb445d1402c8e7af1dabd3312d651b2522e5b Mon Sep 17 00:00:00 2001 From: Kenji Sugimoto Date: Thu, 10 Apr 2014 17:46:23 +0900 Subject: [PATCH] Fix NullPointerException in ListView There is a NullPointerException in `handleHorizontalFocusWithinListItem()` because `selectedView.findFocus()` returns null and then there is no null check when when the assigned variable `currentFocus` is used, although `View.findFocus()` states that it may return null. Change-Id: I6897027e9a2a238d9283e6b9f5146198989fcac0 --- core/java/android/widget/ListView.java | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/core/java/android/widget/ListView.java b/core/java/android/widget/ListView.java index 2e9858c032686..ba6f061a6c186 100644 --- a/core/java/android/widget/ListView.java +++ b/core/java/android/widget/ListView.java @@ -2420,10 +2420,15 @@ public class ListView extends AbsListView { (ViewGroup) selectedView, currentFocus, direction); if (nextFocus != null) { // do the math to get interesting rect in next focus' coordinates - currentFocus.getFocusedRect(mTempRect); - offsetDescendantRectToMyCoords(currentFocus, mTempRect); - offsetRectIntoDescendantCoords(nextFocus, mTempRect); - if (nextFocus.requestFocus(direction, mTempRect)) { + Rect focusedRect = mTempRect; + if (currentFocus != null) { + currentFocus.getFocusedRect(focusedRect); + offsetDescendantRectToMyCoords(currentFocus, focusedRect); + offsetRectIntoDescendantCoords(nextFocus, focusedRect); + } else { + focusedRect = null; + } + if (nextFocus.requestFocus(direction, focusedRect)) { return true; } } @@ -2556,8 +2561,10 @@ public class ListView extends AbsListView { if (mItemsCanFocus && (focusResult == null) && selectedView != null && selectedView.hasFocus()) { final View focused = selectedView.findFocus(); - if (!isViewAncestorOf(focused, this) || distanceToView(focused) > 0) { - focused.clearFocus(); + if (focused != null) { + if (!isViewAncestorOf(focused, this) || distanceToView(focused) > 0) { + focused.clearFocus(); + } } }