Skip to content

Commit 556aa93

Browse files
sunnylqmfacebook-github-bot
authored andcommitted
Prevent crash when setting underlineColorAndroid (#24183)
Summary: Try to prevent the crash described in #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: #24183 Differential Revision: D14710484 Pulled By: cpojer fbshipit-source-id: 3af20a5cb0ecd40839beaf85118c0f5aa6905414
1 parent 4478e50 commit 556aa93

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

ReactAndroid/src/main/java/com/facebook/react/views/textinput/BUCK

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ rn_android_library(
1313
],
1414
deps = [
1515
YOGA_TARGET,
16+
react_native_dep("libraries/fbcore/src/main/java/com/facebook/common/logging:logging"),
1617
react_native_dep("third-party/java/infer-annotations:infer-annotations"),
1718
react_native_dep("third-party/java/jsr-305:jsr-305"),
1819
react_native_target("java/com/facebook/react/bridge:bridge"),

ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputManager.java

+10-4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import android.view.View;
2828
import android.view.inputmethod.EditorInfo;
2929
import android.widget.TextView;
30+
import com.facebook.common.logging.FLog;
3031
import com.facebook.infer.annotation.Assertions;
3132
import com.facebook.react.bridge.JSApplicationIllegalArgumentException;
3233
import com.facebook.react.bridge.ReactContext;
@@ -64,7 +65,7 @@
6465
*/
6566
@ReactModule(name = ReactTextInputManager.REACT_CLASS)
6667
public class ReactTextInputManager extends BaseViewManager<ReactEditText, LayoutShadowNode> {
67-
68+
public static final String TAG = ReactTextInputManager.class.getSimpleName();
6869
protected static final String REACT_CLASS = "AndroidTextInput";
6970

7071
private static final int[] SPACING_TYPES = {
@@ -464,9 +465,14 @@ public void setUnderlineColor(ReactEditText view, @Nullable Integer underlineCol
464465
// Drawable.mutate() can sometimes crash due to an AOSP bug:
465466
// See https://code.google.com/p/android/issues/detail?id=191754 for more info
466467
Drawable background = view.getBackground();
467-
Drawable drawableToMutate = background.getConstantState() != null ?
468-
background.mutate() :
469-
background;
468+
Drawable drawableToMutate = background;
469+
if (background.getConstantState() != null) {
470+
try {
471+
drawableToMutate = background.mutate();
472+
} catch (NullPointerException e) {
473+
FLog.e(TAG, "NullPointerException when setting underlineColorAndroid for TextInput", e);
474+
}
475+
}
470476

471477
if (underlineColor == null) {
472478
drawableToMutate.clearColorFilter();

0 commit comments

Comments
 (0)