com.deliveryrunner.vendor.MainActivity cannot be cast to android.app.Application

Viewed 708

I have an android application made with flutter, in older versions I've used firebase_messaging which required me to change my MainActivity file to Application with some edits mentioned below to be able to handle background messages. and it worked fine back then.

Here is the old Application file :

package com.deliveryrunner.vendor;

import io.flutter.app.FlutterApplication;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback;
import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService;

public class Application extends FlutterApplication implements PluginRegistrantCallback {

    @Override
    public void onCreate() {
        super.onCreate();
        FlutterFirebaseMessagingService.setPluginRegistrant(this);
    }

    @Override
    public void registerWith(PluginRegistry registry) {
        FirebaseCloudMessagingPluginRegistrant.registerWith(registry);
    }

}

Now after upgrading to the latest version of flutter and firebase_messaging they mentioned in the documentation that I don't need this anymore and I can go back to normal MainACtivity and I did that. here is the MainActivity file :

package com.deliveryrunner.vendor

import io.flutter.embedding.android.FlutterActivity

class MainActivity : FlutterActivity() {}

please note that i am changing the entry in AndroidManifest.xml whenever i change the file:

    <application
        android:name=".Application"
    <application
        android:name=".MainActivity"

finally, after upgrading whenever I run the app I get this error :

E/AndroidRuntime(14596): FATAL EXCEPTION: main
E/AndroidRuntime(14596): Process: com.deliveryrunner.vendor, PID: 14596
E/AndroidRuntime(14596): java.lang.RuntimeException: Unable to instantiate application com.deliveryrunner.vendor.MainActivity: java.lang.ClassCastException: com.deliveryrunner.vendor.MainActivity cannot be cast to android.app.Application
E/AndroidRuntime(14596):    at android.app.LoadedApk.makeApplication(LoadedApk.java:1244)
E/AndroidRuntime(14596):    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6683)
E/AndroidRuntime(14596):    at android.app.ActivityThread.access$1300(ActivityThread.java:237)
E/AndroidRuntime(14596):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1913)
E/AndroidRuntime(14596):    at android.os.Handler.dispatchMessage(Handler.java:106)
E/AndroidRuntime(14596):    at android.os.Looper.loop(Looper.java:223)
E/AndroidRuntime(14596):    at android.app.ActivityThread.main(ActivityThread.java:7656)
E/AndroidRuntime(14596):    at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime(14596):    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
E/AndroidRuntime(14596):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
E/AndroidRuntime(14596): Caused by: java.lang.ClassCastException: com.deliveryrunner.vendor.MainActivity cannot be cast to android.app.Application
E/AndroidRuntime(14596):    at android.app.AppComponentFactory.instantiateApplication(AppComponentFactory.java:76)
E/AndroidRuntime(14596):    at androidx.core.app.CoreComponentFactory.instantiateApplication(CoreComponentFactory.java:52)
E/AndroidRuntime(14596):    at android.app.Instrumentation.newApplication(Instrumentation.java:1158)
E/AndroidRuntime(14596):    at android.app.LoadedApk.makeApplication(LoadedApk.java:1236)
E/AndroidRuntime(14596):    ... 9 more


2 Answers

Please remove this line from the Application tag

<application
    android:name=".MainActivity"

Add the line inside the activity tag

<activity
    android:name=".MainActivity"

Please remove this line from the Application tag

<application
    android:name=".MainActivity"

Or

    android:name="io.flutter.embedding.android.FlutterFragmentActivity"

Add the line inside the activity tag

<activity
    android:name=".MainActivity"

Similar to this code

 <application
    android:label="..."
    android:icon="@mipmap/logo">
    <activity
        android:name=".MainActivity"
        android:exported="true"
    

This code is work in my case. Thanks for helping me

Related