From a8011bbfdc3f636e4c7d06261bb9b32f220eea70 Mon Sep 17 00:00:00 2001 From: James Ide Date: Thu, 30 Jun 2016 20:46:43 -0700 Subject: [PATCH] Set LayoutParams in ReactTextView to fix crash Summary: ReactTextView occasionally crashes when `setText` is called. Doing some cursory research, it looks like this could be a bug in Android. We also suspect it might be related to removeClippedSubviews though. The LayoutParams don't actually matter because RN controls layout, but on occasion (haven't narrowed down what this is...) `mLayout` is non-null and triggers relayout during `setText`, which fails because the LayoutParams are null. jesseruder came up with this fix and it appears to be working well. Stack trace: http://pastebin.com/P8VbxvPz Closes https://github.com/facebook/react-native/pull/7011 Differential Revision: D3508097 fbshipit-source-id: 12b4aa11e42112c8ba19a1af325e3ee9a232d08f --- .../com/facebook/react/views/text/ReactTextView.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextView.java b/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextView.java index 11f1aee50e865c..2d942d8196773b 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextView.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextView.java @@ -14,12 +14,16 @@ import android.text.Layout; import android.text.Spanned; import android.view.Gravity; +import android.view.ViewGroup; import android.widget.TextView; import com.facebook.react.uimanager.ReactCompoundView; public class ReactTextView extends TextView implements ReactCompoundView { + private static final ViewGroup.LayoutParams EMPTY_LAYOUT_PARAMS = + new ViewGroup.LayoutParams(0, 0); + private boolean mContainsImages; private int mDefaultGravityHorizontal; private int mDefaultGravityVertical; @@ -34,6 +38,12 @@ public ReactTextView(Context context) { public void setText(ReactTextUpdate update) { mContainsImages = update.containsImages(); + // Android's TextView crashes when it tries to relayout if LayoutParams are + // null; explicitly set the LayoutParams to prevent this crash. See: + // https://github.com/facebook/react-native/pull/7011 + if (getLayoutParams() == null) { + setLayoutParams(EMPTY_LAYOUT_PARAMS); + } setText(update.getText()); }