场景
Flutter版本更新后,运行出现以下问题,App闪退。
W/FlutterEngineCxnRegstry(10678): Attempted to register plugin (io.flutter.embedding.engine.plugins.shim.ShimPluginRegistry$ShimRegistrarAggregate@3777b26) but it was already registered with this FlutterEngine (io.flutter.embedding.engine.FlutterEngine@6c93367).
E/AndroidRuntime(10678): FATAL EXCEPTION: main
E/AndroidRuntime(10678): Process: com.example.firstflutterapp, PID: 10678
E/AndroidRuntime(10678): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.firstflutterapp/com.example.firstflutterapp.MainActivity}: java.lang.NullPointerException: Attempt to invoke interface method 'void io.flutter.plugin.common.BinaryMessenger.setMessageHandler(java.lang.String, io.flutter.plugin.common.BinaryMessenger$BinaryMessageHandler)' on a null object reference
E/AndroidRuntime(10678): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3898)
E/AndroidRuntime(10678): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4081)
E/AndroidRuntime(10678): at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:91)
E/AndroidRuntime(10678): at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:149)
E/AndroidRuntime(10678): at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:103)
E/AndroidRuntime(10678): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2462)
E/AndroidRuntime(10678): at android.os.Handler.dispatchMessage(Handler.java:110)
E/AndroidRuntime(10678): at android.os.Looper.loop(Looper.java:219)
E/AndroidRuntime(10678): at android.app.ActivityThread.main(ActivityThread.java:8393)
E/AndroidRuntime(10678): at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime(10678): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
E/AndroidRuntime(10678): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1055)
E/AndroidRuntime(10678): Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'void io.flutter.plugin.common.BinaryMessenger.setMessageHandler(java.lang.String, io.flutter.plugin.common.BinaryMessenger$BinaryMessageHandler)' on a null object reference
E/AndroidRuntime(10678): at io.flutter.plugin.common.MethodChannel.setMethodCallHandler(MethodChannel.java:119)
E/AndroidRuntime(10678): at com.jzoom.amaplocation.AmapLocationPlugin.registerWith(AmapLocationPlugin.java:54)
E/AndroidRuntime(10678): at io.flutter.plugins.GeneratedPluginRegistrant.registerWith(GeneratedPluginRegistrant.java:18)
E/AndroidRuntime(10678): at com.example.firstflutterapp.MainActivity.configureFlutterEngine(MainActivity.kt:31)
E/AndroidRuntime(10678): at io.flutter.embedding.android.FlutterActivityAndFragmentDelegate.onAttach(FlutterActivityAndFragmentDelegate.java:180)
E/AndroidRuntime(10678): at io.flutter.embedding.android.FlutterActivity.onCreate(FlutterActivity.java:409)
E/AndroidRuntime(10678): at android.app.Activity.performCreate(Activity.java:8121)
E/AndroidRuntime(10678): at android.app.Activity.performCreate(Activity.java:8109)
E/AndroidRuntime(10678): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1320)
E/AndroidRuntime(10678): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3871)
E/AndroidRuntime(10678): ... 11 more
解决方案
根据错误描述,是因为注册plugin时提示已经被注册过了了。
涉及到插件,需要看官方文档。https://github.com/flutter/flutter/wiki/Upgrading-pre-1.12-Android-projects。
以下是我的MainActivity.kt
class MainActivity: FlutterActivity() {
override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
GeneratedPluginRegistrant.registerWith(flutterEngine)
}
}
这是官方代码
+ @Override
+ public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
+ GeneratedPluginRegistrant.registerWith(flutterEngine);
+ new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), CHANNEL)
+ .setMethodCallHandler(
+ (call, result) -> {
+ // Your existing code
+ }
+ );
+ }
发现多了个super方法,查看FlutterActivity源码发现,configureFlutterEngine方法已经调用过了GeneratedPluginRegistrant.registerWith(flutterEngine),所以我们只需要删除super.configureFlutterEngine(flutterEngine)即可。
class MainActivity: FlutterActivity() {
override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
//super.configureFlutterEngine(flutterEngine)
GeneratedPluginRegistrant.registerWith(flutterEngine)
}
}