Skip to content

Commit 9cd2905

Browse files
authored
Fix NullPointerException when evaluating hitSlop on Android (#2241)
## Description Fixes #2239. Based on the location of the crash from the issue this seems to be a concurrency issue. Using `?.let` instead of non-null assertions should resolve the problem, as the used variable will be guaranteed not to change while the block is being executed. ## Test plan I don't really know how to reproduce it, but I also don't see what could be the other solution, so... trust me?
1 parent 8254e4a commit 9cd2905

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

android/lib/src/main/java/com/swmansion/gesturehandler/GestureHandler.kt

+7-7
Original file line numberDiff line numberDiff line change
@@ -589,11 +589,11 @@ open class GestureHandler<ConcreteGestureHandlerT : GestureHandler<ConcreteGestu
589589
var top = 0f
590590
var right = view!!.width.toFloat()
591591
var bottom = view.height.toFloat()
592-
if (hitSlop != null) {
593-
val padLeft = hitSlop!![HIT_SLOP_LEFT_IDX]
594-
val padTop = hitSlop!![HIT_SLOP_TOP_IDX]
595-
val padRight = hitSlop!![HIT_SLOP_RIGHT_IDX]
596-
val padBottom = hitSlop!![HIT_SLOP_BOTTOM_IDX]
592+
hitSlop?.let { hitSlop ->
593+
val padLeft = hitSlop[HIT_SLOP_LEFT_IDX]
594+
val padTop = hitSlop[HIT_SLOP_TOP_IDX]
595+
val padRight = hitSlop[HIT_SLOP_RIGHT_IDX]
596+
val padBottom = hitSlop[HIT_SLOP_BOTTOM_IDX]
597597
if (hitSlopSet(padLeft)) {
598598
left -= padLeft
599599
}
@@ -606,8 +606,8 @@ open class GestureHandler<ConcreteGestureHandlerT : GestureHandler<ConcreteGestu
606606
if (hitSlopSet(padBottom)) {
607607
bottom += padBottom
608608
}
609-
val width = hitSlop!![HIT_SLOP_WIDTH_IDX]
610-
val height = hitSlop!![HIT_SLOP_HEIGHT_IDX]
609+
val width = hitSlop[HIT_SLOP_WIDTH_IDX]
610+
val height = hitSlop[HIT_SLOP_HEIGHT_IDX]
611611
if (hitSlopSet(width)) {
612612
if (!hitSlopSet(padLeft)) {
613613
left = right - width

0 commit comments

Comments
 (0)