From 15656342a8401eb599090da7962928dd48d7d890 Mon Sep 17 00:00:00 2001 From: Alpha Date: Fri, 18 Nov 2022 04:06:32 -0800 Subject: [PATCH] Resolve android crash on display modal (#35380) Summary: From exception logging, found crashes due to `Attempt to invoke virtual method 'int android.view.ViewGroup.getChildCount()' on a null object reference`. Tracing through the stack, it appears the constructor for `ViewGroup` conditionally calls `initializeScrollbarsInternal()`, which in turn calls `getChildCount()`. However `ReactModalHostView` overrides `getChildCount()`, so `getChildCount()` is called before `ReactModalHostView` constructor completes, resulting in null reference when accessing `mHostView` from `getChildCount()`. ## Changelog [Android] [Fixed] - Fix crash on initialize modal Pull Request resolved: https://github.com/facebook/react-native/pull/35380 Test Plan: In the rn-tester project, display a modal. Reviewed By: javache, cipolleschi Differential Revision: D41392235 Pulled By: ryancat fbshipit-source-id: ce78e4d458ad41769e78139ea0a8a038384e830d --- .../com/facebook/react/views/modal/ReactModalHostView.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/modal/ReactModalHostView.java b/ReactAndroid/src/main/java/com/facebook/react/views/modal/ReactModalHostView.java index 82be75a4377fc9..b8046f5da76c21 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/modal/ReactModalHostView.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/modal/ReactModalHostView.java @@ -120,7 +120,9 @@ public void addView(View child, int index) { @Override public int getChildCount() { - return mHostView.getChildCount(); + // This method may be called by the parent constructor + // before mHostView is initialized. + return mHostView == null ? 0 : mHostView.getChildCount(); } @Override