electrode-native icon indicating copy to clipboard operation
electrode-native copied to clipboard

When running on Android, error: Mini App cannot be cast to androidx.fragment.app.FragmentActivity

Open marc-hughes opened this issue 5 years ago • 5 comments

I am unable to get a mini app running inside an external android project, and consistently get this error:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.rosettastone.testapp, PID: 29470
    java.lang.ClassCastException: com.walmartlabs.ern.container.miniapps.TestMiniAppActivity cannot be cast to androidx.fragment.app.FragmentActivity
        at com.facebook.react.modules.dialog.DialogModule.getFragmentManagerHelper(DialogModule.java:245)
        at com.facebook.react.modules.dialog.DialogModule.onHostResume(DialogModule.java:177)
        at com.facebook.react.bridge.ReactContext$1.run(ReactContext.java:174)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:29)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

To reproduce:

  1. Start a new ern mini app ern create-miniapp test
  2. Create a new android project in android studio, selecting "Basic Activity" and the defaults from there on.
  3. Run ern run-android, verify the application correctly launches
  4. Run the android project from android studio, verify it launches
  5. Publish the mini app ern publish-container --platform android -p ern-container-publisher-maven -e publish.json
  6. Integrate the mini app into the android app (see https://github.com/marc-hughes/electrode-native-fragment-bug-report for full code)
  7. Run the android app, get the error above.

The cause appears to be that react native's DialogModule attempts to do this:

  private @Nullable FragmentManagerHelper getFragmentManagerHelper() {
    Activity activity = getCurrentActivity();
    if (activity == null) {
      return null;
    }
    return new FragmentManagerHelper(((FragmentActivity) activity).getSupportFragmentManager());
  }

And the mini app does not inherit from FragmentActivity.

It looks like this would have been an issue since https://github.com/facebook/react-native/pull/23365

ern --version
 ___ _        _               _       _  _      _   _
| __| |___ __| |_ _ _ ___  __| |___  | \| |__ _| |_(_)_ _____
| _|| / -_) _|  _| '_/ _ \/ _` / -_) | .` / _` |  _| \ V / -_)
|___|_\___\__|\__|_| \___/\__,_\___| |_|\_\__,_|\__|_|\_/\___|
[v0.38.2] [Cauldron: tutoring-cauldron]

ℹ ern-local-cli : 0.38.2
ℹ electrode-native : 1.0.17

marc-hughes avatar Oct 11 '19 17:10 marc-hughes

@marc-hughes Good catch. ElectrodeMiniAppActivity has not been updated to use the new way we want our MiniApp to be launched. We will update this soon. It is going to be something similar to this MainActivity. In your sample, I would recommend creating an activity similar to this MainActivity instead of using the TestMiniAppActivity. Thanks for reporting this.

deepueg avatar Oct 11 '19 18:10 deepueg

Thanks a lot for the help @deepueg

For anyone else encountering this, here's the changes I made to get it working: https://github.com/marc-hughes/electrode-native-fragment-bug-report/pull/1/files

marc-hughes avatar Oct 11 '19 18:10 marc-hughes

This problem also appeared in integration-with-existing-apps.

2019-10-12 09:57:20.982 18495-18495/com***** E/unknown:ReactNative: Exception in native call
    java.lang.ClassCastException: com*****.MainActivity cannot be cast to androidx.fragment.app.FragmentActivity
        at com.facebook.react.modules.dialog.DialogModule.getFragmentManagerHelper(DialogModule.java:245)
        at com.facebook.react.modules.dialog.DialogModule.onHostResume(DialogModule.java:177)
        at com.facebook.react.bridge.ReactContext.onHostResume(ReactContext.java:208)
        at com.facebook.react.ReactInstanceManager.moveToResumedLifecycleState(ReactInstanceManager.java:661)
        at com.facebook.react.ReactInstanceManager.onHostResume(ReactInstanceManager.java:580)
        at com.facebook.react.ReactInstanceManager.onHostResume(ReactInstanceManager.java:536)
        at com.*****.MainActivity.onResume(MainActivity.java:4227)
        at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1355)
        at android.app.Activity.performResume(Activity.java:7154)
        at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3666)
        at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3731)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2937)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1620)
        at android.os.Handler.dispatchMessage(Handler.java:105)
        at android.os.Looper.loop(Looper.java:176)
        at android.app.ActivityThread.main(ActivityThread.java:6704)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:249)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:783)

//  com.*****.MainActivity
public class MainActivity extends NativeActivity implements **** {
  ....
  protected void onResume() {
    .....
    ......
    mReactNativeHost.getReactInstanceManager().onHostResume(this, this);
    ......
  }
}

react-native: 0.60.5 react: 16.8.6

What 's wrong with the code? @deepueg @marc-hughes Can you give me any ideas please? Thank you

woshi82 avatar Oct 12 '19 02:10 woshi82

@woshi82 Since the NativeActivity extends Activity and not FragemntActivity you will have this error. Not sure why this was changed on the ReactNative side. I think this is going to break everyone who has custom RN integrations without a FragmentActivity.

deepueg avatar Oct 14 '19 21:10 deepueg

Any solution to this? I have the same problem:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.myapplication, PID: 32400
    java.lang.RuntimeException: Unable to resume activity {com.example.myapplication/com.walmartlabs.ern.container.miniapps.SdkMobileMiniAppActivity}: java.lang.ClassCastException: com.walmartlabs.ern.container.miniapps.SdkMobileMiniAppActivity cannot be cast to androidx.fragment.app.FragmentActivity
        at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3103)
        at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3134)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2481)
        at android.app.ActivityThread.-wrap11(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5417)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
     Caused by: java.lang.ClassCastException: com.walmartlabs.ern.container.miniapps.SdkMobileMiniAppActivity cannot be cast to androidx.fragment.app.FragmentActivity
        at com.facebook.react.modules.dialog.DialogModule.getFragmentManagerHelper(DialogModule.java:245)
        at com.facebook.react.modules.dialog.DialogModule.onHostResume(DialogModule.java:177)
        at com.facebook.react.bridge.ReactContext.onHostResume(ReactContext.java:208)
        at com.facebook.react.ReactInstanceManager.moveToResumedLifecycleState(ReactInstanceManager.java:661)
        at com.facebook.react.ReactInstanceManager.onHostResume(ReactInstanceManager.java:580)
        at com.facebook.react.ReactInstanceManager.onHostResume(ReactInstanceManager.java:536)
        at com.walmartlabs.ern.container.ElectrodeReactActivityDelegate.onResume(ElectrodeReactActivityDelegate.java:236)
        at com.walmartlabs.ern.container.ElectrodeMiniAppActivity.onResume(ElectrodeMiniAppActivity.java:72)
        at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1258)
        at android.app.Activity.performResume(Activity.java:6312)
        at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3092)
        at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3134) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2481) 
        at android.app.ActivityThread.-wrap11(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:148) 
        at android.app.ActivityThread.main(ActivityThread.java:5417) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

React Native: 0.60.6 Electrode: 0.40.0 Publish method: git GitHub generated android: https://github.com/marcosjuniorzup/sdk-web-publisher

marcosjuniorzup avatar Mar 26 '20 15:03 marcosjuniorzup