From 224ec7e761c94b7f7c0838cb5d905b7b748f2285 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=8A=B2=E9=B9=8F?= Date: Tue, 27 Jul 2021 13:52:36 +0800 Subject: [PATCH] Android: fix text layout npe --- .../src/main/java/pub/doric/shader/TextNode.java | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/doric-android/doric/src/main/java/pub/doric/shader/TextNode.java b/doric-android/doric/src/main/java/pub/doric/shader/TextNode.java index 333f72c7..d7f23200 100644 --- a/doric-android/doric/src/main/java/pub/doric/shader/TextNode.java +++ b/doric-android/doric/src/main/java/pub/doric/shader/TextNode.java @@ -27,6 +27,7 @@ import android.text.Layout; import android.text.TextUtils; import android.util.TypedValue; import android.view.Gravity; +import android.view.ViewTreeObserver; import android.widget.TextView; import androidx.core.content.res.ResourcesCompat; @@ -95,9 +96,11 @@ public class TextNode extends ViewNode { if (prop.isNumber()) { view.setTextColor(prop.asNumber().toInt()); } else if (prop.isObject()) { - view.post(new Runnable() { + view.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { @Override - public void run() { + public boolean onPreDraw() { + view.getViewTreeObserver().removeOnPreDrawListener(this); + final JSObject dict = prop.asObject(); int[] colors = null; @@ -164,8 +167,11 @@ public class TextNode extends ViewNode { } setGradientTextColor(view, angle, colors, locations); + + return true; } }); + view.invalidate(); } break; case "textAlignment": @@ -321,6 +327,11 @@ public class TextNode extends ViewNode { final Rect textBound = new Rect(Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MIN_VALUE, Integer.MIN_VALUE); final Layout layout = textView.getLayout(); + + if (layout == null) { + return; + } + for (int i = 0; i < textView.getLineCount(); i++) { float left = layout.getLineLeft(i); float right = layout.getLineRight(i);