From 556aa93ed72d9dc0f18a1c6d7dec3d9c182fee85 Mon Sep 17 00:00:00 2001 From: Sunny Luo Date: Tue, 2 Apr 2019 09:36:30 -0700 Subject: [PATCH] Prevent crash when setting underlineColorAndroid (#24183) Summary: Try to prevent the crash described in https://github.com/facebook/react-native/issues/17530 There seems to be a bug in `Drawable.mutate()` in some devices/android os versions even after you check the constant state. This error is hard to reproduce and to fix, so just try to catch the exception to prevent crash. [Android][Fixed] Prevent random crash when setting underlineColorAndroid Pull Request resolved: https://github.com/facebook/react-native/pull/24183 Differential Revision: D14710484 Pulled By: cpojer fbshipit-source-id: 3af20a5cb0ecd40839beaf85118c0f5aa6905414 --- .../java/com/facebook/react/views/textinput/BUCK | 1 + .../views/textinput/ReactTextInputManager.java | 14 ++++++++++---- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/textinput/BUCK b/ReactAndroid/src/main/java/com/facebook/react/views/textinput/BUCK index dae8c212ad0913..2ce3100e90fc45 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/textinput/BUCK +++ b/ReactAndroid/src/main/java/com/facebook/react/views/textinput/BUCK @@ -13,6 +13,7 @@ rn_android_library( ], deps = [ YOGA_TARGET, + react_native_dep("libraries/fbcore/src/main/java/com/facebook/common/logging:logging"), react_native_dep("third-party/java/infer-annotations:infer-annotations"), react_native_dep("third-party/java/jsr-305:jsr-305"), react_native_target("java/com/facebook/react/bridge:bridge"), diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputManager.java b/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputManager.java index 5788161bd28512..60392088f26a6a 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputManager.java @@ -27,6 +27,7 @@ import android.view.View; import android.view.inputmethod.EditorInfo; import android.widget.TextView; +import com.facebook.common.logging.FLog; import com.facebook.infer.annotation.Assertions; import com.facebook.react.bridge.JSApplicationIllegalArgumentException; import com.facebook.react.bridge.ReactContext; @@ -64,7 +65,7 @@ */ @ReactModule(name = ReactTextInputManager.REACT_CLASS) public class ReactTextInputManager extends BaseViewManager { - + public static final String TAG = ReactTextInputManager.class.getSimpleName(); protected static final String REACT_CLASS = "AndroidTextInput"; private static final int[] SPACING_TYPES = { @@ -464,9 +465,14 @@ public void setUnderlineColor(ReactEditText view, @Nullable Integer underlineCol // Drawable.mutate() can sometimes crash due to an AOSP bug: // See https://code.google.com/p/android/issues/detail?id=191754 for more info Drawable background = view.getBackground(); - Drawable drawableToMutate = background.getConstantState() != null ? - background.mutate() : - background; + Drawable drawableToMutate = background; + if (background.getConstantState() != null) { + try { + drawableToMutate = background.mutate(); + } catch (NullPointerException e) { + FLog.e(TAG, "NullPointerException when setting underlineColorAndroid for TextInput", e); + } + } if (underlineColor == null) { drawableToMutate.clearColorFilter();