From 35d9da8f67911c84267fd0829fea6d5aba7c3544 Mon Sep 17 00:00:00 2001 From: conradchen Date: Fri, 3 Dec 2021 19:16:26 +0000 Subject: [PATCH] [SnackBar] Fix NPE on Android 8 & 9 It seems like on Android 8 & 9 framework's View implementation has a bug that under certain scenarios if we set a view's visibility when view.getParent() is null, it can cause NPE crash. Fixes this by switching the order of addView and setVisibility() call. Resolves https://github.com/material-components/material-components-android/issues/917 PiperOrigin-RevId: 413978332 --- .../android/material/snackbar/BaseTransientBottomBar.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/java/com/google/android/material/snackbar/BaseTransientBottomBar.java b/lib/java/com/google/android/material/snackbar/BaseTransientBottomBar.java index 449a3fc3b0a..4a6b8ef85a8 100644 --- a/lib/java/com/google/android/material/snackbar/BaseTransientBottomBar.java +++ b/lib/java/com/google/android/material/snackbar/BaseTransientBottomBar.java @@ -765,10 +765,11 @@ public void run() { recalculateAndUpdateMargins(); + targetParent.addView(this.view); + // Set view to INVISIBLE so it doesn't flash on the screen before the inset adjustment is // handled and the enter animation is started view.setVisibility(View.INVISIBLE); - targetParent.addView(this.view); } if (ViewCompat.isLaidOut(this.view)) {