Skip to content

Commit

Permalink
[Text] Set LayoutParams in ReactTextView to fix crash
Browse files Browse the repository at this point in the history
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.

Stack trace: http://pastebin.com/P8VbxvPz

Test Plan: Run an Android app that was sporadically crashing with the stack trace linked above. With this patch the crashes appear to have disappeared and the app is performing fine.
  • Loading branch information
ide committed May 10, 2016
1 parent d3e5a96 commit 8a8312c
Showing 1 changed file with 10 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());
}

Expand Down

0 comments on commit 8a8312c

Please sign in to comment.