Skip to content

Commit

Permalink
Set LayoutParams in ReactTextView to fix crash
Browse files Browse the repository at this point in the history
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 facebook#7011

Differential Revision: D3508097

fbshipit-source-id: 12b4aa11e42112c8ba19a1af325e3ee9a232d08f
  • Loading branch information
ide authored and Facebook Github Bot 2 committed Jul 1, 2016
1 parent d1d9045 commit a8011bb
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 a8011bb

Please sign in to comment.