Home » Android » Flutter application crashes when providing custom application class in Android's Manifest file-Exceptionshub

Flutter application crashes when providing custom application class in Android's Manifest file-Exceptionshub

Posted by: admin February 26, 2020 Leave a comment

Questions:

I am working on a Flutter application. I have to initialize an object in Application class in Android, and I have created a custom application class that is extending FlutterApplication class. I have provided the class name in Manifest file too but when I run the application it crashes.

Please suggest, how can I do that. Below is the code of application class and manifest file.

Thank You.

Manifest file

 <application
    android:name="com.example.app.MyApplication"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name">
 ...
 </application>

MyApplication class

package com.example.app;
import android.app.Application;
import io.flutter.view.FlutterMain
import io.flutter.app.FlutterApplication;

class MyApplication extends FlutterApplication {

    @override
    public void onCreate() {
        FlutterMain.startInitialization(this)

    }
}
How to&Answers:

The application class handles the context, environment setup etc during app start up process so along with manifest registration, you must call super.onCreate() as stated:

If you override this method, be sure to call {@code
super.onCreate()}.

so do

class MyApplication extends FlutterApplication {

    @override
    public void onCreate() {
        super.onCreate() // add this          
    }
}

Note: With super.onCreate(), there’s no need to call FlutterMain.startInitialization(this), as will be done by FlutterApplication

public class Application extends FlutterApplication implements PluginRegistrantCallback {
  @Override
  public void onCreate() {
    super.onCreate();
    AlarmService.setPluginRegistrant(this);
    // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  }

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