Skip to content

Commit c11e0f5

Browse files
aleqsiokelset
authored andcommitted
Fix invalid context cast in ReactRootView (#36121)
Summary: Hi 👋 I'm one of the Expo contributors. When upgrading the Stripe dependency in Expo for SDK48 release on Android, we noticed the following error: ```java.lang.ClassCastException: android.view.ContextThemeWrapper cannot be cast to android.app.Activity``` With Kudo we narrowed it down to an underlying issue in the following cast in `ReactRootView` done during checking for keyboard events: ```((Activity) getContext())``` The `getContext()` is actually a `ContextThemeWrapper` for `ExperienceActivity`, so we should not cast it to an `Activity` directly - instead, we unwrap it using `getBaseContext()`. Implementing the following fix into the Expo fork fixed the crash for us: expo@0e2c9ca ## Changelog [ANDROID] [FIXED] - Fixed crash occurring in certain native views when handling keyboard events. <!-- Help reviewers and the release process by writing your own changelog entry. Pick one each for the category and type tags: [ANDROID|GENERAL|IOS|INTERNAL] [BREAKING|ADDED|CHANGED|DEPRECATED|REMOVED|FIXED|SECURITY] - Message For more details, see: https://reactnative.dev/contributing/changelogs-in-pull-requests --> Pull Request resolved: #36121 Test Plan: Tested manually by comparing two builds before and after this change. The main branch build crashes when the [Stripe 0.23.1](https://github.com/stripe/stripe-react-native) Card Element from the example app is mounted on screen. Applying the change fixed the issue. Happy to make a more isolated reproduction if necessary. --- Full stack trace: ``` AndroidRuntime D Shutting down VM E FATAL EXCEPTION: main E Process: host.exp.exponent, PID: 8849 E java.lang.ClassCastException: android.view.ContextThemeWrapper cannot be cast to android.app.Activity E at com.facebook.react.ReactRootView$CustomGlobalLayoutListener.checkForKeyboardEvents(ReactRootView.java:937) E at com.facebook.react.ReactRootView$CustomGlobalLayoutListener.onGlobalLayout(ReactRootView.java:913) E at android.view.ViewTreeObserver.dispatchOnGlobalLayout(ViewTreeObserver.java:1061) E at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:3352) E at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:2286) E at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:8948) E at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1231) E at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1239) E at android.view.Choreographer.doCallbacks(Choreographer.java:899) E at android.view.Choreographer.doFrame(Choreographer.java:832) E at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1214) E at android.os.Handler.handleCallback(Handler.java:942) E at android.os.Handler.dispatchMessage(Handler.java:99) E at android.os.Looper.loopOnce(Looper.java:201) E at android.os.Looper.loop(Looper.java:288) E at android.app.ActivityThread.main(ActivityThread.java:7898) E at java.lang.reflect.Method.invoke(Native Method) E at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548) E at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936) ``` Reviewed By: cortinico Differential Revision: D43186530 Pulled By: javache fbshipit-source-id: 2143495f6b0c71f342eba6d5abb2bfa4a529fbdd
1 parent fd8a19d commit c11e0f5

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

ReactAndroid/src/main/java/com/facebook/react/ReactRootView.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import android.app.Activity;
1616
import android.content.Context;
17+
import android.content.ContextWrapper;
1718
import android.graphics.Canvas;
1819
import android.graphics.Insets;
1920
import android.graphics.Point;
@@ -916,6 +917,14 @@ public void onGlobalLayout() {
916917
checkForDeviceDimensionsChanges();
917918
}
918919

920+
private Activity getActivity() {
921+
Context context = getContext();
922+
while (!(context instanceof Activity) && context instanceof ContextWrapper) {
923+
context = ((ContextWrapper) context).getBaseContext();
924+
}
925+
return (Activity) context;
926+
}
927+
919928
@RequiresApi(api = Build.VERSION_CODES.R)
920929
private void checkForKeyboardEvents() {
921930
getRootView().getWindowVisibleDisplayFrame(mVisibleViewArea);
@@ -933,7 +942,7 @@ private void checkForKeyboardEvents() {
933942
Insets barInsets = rootInsets.getInsets(WindowInsets.Type.systemBars());
934943
int height = imeInsets.bottom - barInsets.bottom;
935944

936-
int softInputMode = ((Activity) getContext()).getWindow().getAttributes().softInputMode;
945+
int softInputMode = getActivity().getWindow().getAttributes().softInputMode;
937946
int screenY =
938947
softInputMode == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING
939948
? mVisibleViewArea.bottom - height

0 commit comments

Comments
 (0)