Flutter Android ERROR: ensureInitializationComplete must be called after startInitialization

今天在flutter的开发中出现这么一个错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testappandroid/io.flutter.embedding.android.FlutterActivity}: java.lang.IllegalStateException: ensureInitializationComplete must be called after startInitialization
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
     Caused by: java.lang.IllegalStateException: ensureInitializationComplete must be called after startInitialization
        at io.flutter.view.FlutterMain.ensureInitializationComplete(FlutterMain.java:168)
        at io.flutter.embedding.android.FlutterActivityAndFragmentDelegate.initializeFlutter(FlutterActivityAndFragmentDelegate.java:177)
        at io.flutter.embedding.android.FlutterActivityAndFragmentDelegate.onAttach(FlutterActivityAndFragmentDelegate.java:140)
        at io.flutter.embedding.android.FlutterActivity.onCreate(FlutterActivity.java:399)
        at android.app.Activity.performCreate(Activity.java:7136)
        at android.app.Activity.performCreate(Activity.java:7127)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)

在堆栈溢出的网站恰巧有人也遇到,里面有人给出了答案 原文地址

解决这个bug只需要添加一行代码 

override fun onCreate(savedInstanceState: Bundle?) {
    FlutterMain.startInitialization(this) //在这个位置添加这一行即可
    super.onCreate(savedInstanceState)
    GeneratedPluginRegistrant.registerWith(this)
}

 

posted @ 2020-10-12 12:16  在云端i  阅读(962)  评论(4编辑  收藏  举报