Skip to content

Commit 52a3055

Browse files
Kenji Sugimotocodex-corp
authored andcommitted
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
1 parent fdd1ae2 commit 52a3055

1 file changed

Lines changed: 13 additions & 6 deletions

File tree

core/java/android/widget/ListView.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2385,10 +2385,15 @@ private boolean handleHorizontalFocusWithinListItem(int direction) {
23852385
(ViewGroup) selectedView, currentFocus, direction);
23862386
if (nextFocus != null) {
23872387
// do the math to get interesting rect in next focus' coordinates
2388-
currentFocus.getFocusedRect(mTempRect);
2389-
offsetDescendantRectToMyCoords(currentFocus, mTempRect);
2390-
offsetRectIntoDescendantCoords(nextFocus, mTempRect);
2391-
if (nextFocus.requestFocus(direction, mTempRect)) {
2388+
Rect focusedRect = mTempRect;
2389+
if (currentFocus != null) {
2390+
currentFocus.getFocusedRect(focusedRect);
2391+
offsetDescendantRectToMyCoords(currentFocus, focusedRect);
2392+
offsetRectIntoDescendantCoords(nextFocus, focusedRect);
2393+
} else {
2394+
focusedRect = null;
2395+
}
2396+
if (nextFocus.requestFocus(direction, focusedRect)) {
23922397
return true;
23932398
}
23942399
}
@@ -2521,8 +2526,10 @@ private boolean arrowScrollImpl(int direction) {
25212526
if (mItemsCanFocus && (focusResult == null)
25222527
&& selectedView != null && selectedView.hasFocus()) {
25232528
final View focused = selectedView.findFocus();
2524-
if (!isViewAncestorOf(focused, this) || distanceToView(focused) > 0) {
2525-
focused.clearFocus();
2529+
if (focused != null) {
2530+
if (!isViewAncestorOf(focused, this) || distanceToView(focused) > 0) {
2531+
focused.clearFocus();
2532+
}
25262533
}
25272534
}
25282535

0 commit comments

Comments
 (0)