Skip to content

Commit 0e2c9ca

Browse files
committed
[android] fix ReactRootView cast error happening on stripe sdk (#31)
when testing stripe sdk example "Card element only" test case, there is an exception from keyboard listener ``` 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) ``` the `getContext()` is actually a `ContextThemeWrapper` for `ExperienceActivity`, we should not cast it to Activity directly. this pr tries to use `getBaseContext()` to unwrap it.
1 parent b02f31b commit 0e2c9ca

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)