electrode-native
electrode-native copied to clipboard
Error when initializing ElectrodeReactContainer
I already apologize for my English not being so good, I'm from Brazil, and I'm migrating an application that we have in native (iOS and Android) to React Native. In my miniapp I am using git approach, and during application startup after putting:
ElectrodeReactContainer.initialize(
this as Application,
ElectrodeReactContainer.Config()
)
My application does not open and trigger this error, I'm having this error:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapplication, PID: 31902
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapplication/com.example.myapplication.MainActivity}: java.lang.ClassCastException: com.example.myapplication.MainActivity cannot be cast to android.app.Application
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
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.example.myapplication.MainActivity cannot be cast to android.app.Application
at com.example.myapplication.MainActivity.onCreate(MainActivity.kt:21)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
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)
My generated submodule: https://github.com/marcosjuniorzup/sdk-web-publisher My main activity:
package com.example.myapplication
import android.app.Application
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import com.walmartlabs.ern.container.miniapps.SdkMobileMiniAppActivity
import com.walmartlabs.ern.container.ElectrodeReactContainer
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
ElectrodeReactContainer.initialize(
this as Application,
ElectrodeReactContainer.Config()
)
val button = findViewById(R.id.main_button) as Button
button.setOnClickListener {
val intent = Intent(this, SdkMobileMiniAppActivity::class.java)
startActivity(intent)
}
}
}
If i change this as Application
to this.application
another error is trigged
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapplication, PID: 32013
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)
The first param should really be an instance of the Application class (see: Application). Just doing an unsafe cast using as! Application
will only fool the compiler and will not make it an Application instance.
Personally, I've just put the initialization of the container in my custom application class. You probably already have such a class in the app. If this, might be useful:
Hi @marcosjuniorzup - @LcTwisk is correct, you cannot cast an Activity to an Application.
You should move the following section out of your Activity
class, and into your Application
class:
// MainActivity.kt
//...
ElectrodeReactContainer.initialize(
this as Application,
ElectrodeReactContainer.Config()
)
// App.kt
class App : Application() {
override fun onCreate() {
super.onCreate()
ElectrodeReactContainer.initialize(
this,
ElectrodeReactContainer.Config()
)
}
}
If com.example.myapplication
does not have a file for the application class yet, you need to add it first (and also declare it in AndroidManifest.xml
).
I already changed this, and put it in the issue, but I'm still having problems with Fragment, I had to do this workaround https://github.com/electrode-io/electrode-native/issues/1363
@marcosjuniorzup - Can you post some more details about the problems with Fragment?