闪退报错如下:
E/DEBUG: java.lang.RuntimeException: Unable to stop service id.flutter.flutter_background_service.BackgroundService@d8e898a: java.lang.NullPointerException: Attempt to invoke virtual method 'void io.flutter.plugin.common.MethodChannel.setMethodCallHandler(io.flutter.plugin.common.MethodChannel$MethodCallHandler)' on a null object reference
E/DEBUG: at android.app.ActivityThread.handleStopService(ActivityThread.java:5006)
E/DEBUG: at android.app.ActivityThread.-$$Nest$mhandleStopService(Unknown Source:0)
E/DEBUG: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2304)
E/DEBUG: at android.os.Handler.dispatchMessage(Handler.java:106)
E/DEBUG: at android.os.Looper.loopOnce(Looper.java:211)
E/DEBUG: at android.os.Looper.loop(Looper.java:300)
E/DEBUG: at android.app.ActivityThread.main(ActivityThread.java:8503)
E/DEBUG: at java.lang.reflect.Method.invoke(Native Method)
E/DEBUG: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:561)
E/DEBUG: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:954)
E/DEBUG: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void io.flutter.plugin.common.MethodChannel.setMethodCallHandler(io.flutter.plugin.common.MethodChannel$MethodCallHandler)' on a null object reference
E/DEBUG: at com.leeson.image_pickers.ImagePickersPlugin.onDetachedFromEngine(ImagePickersPlugin.java:295)
E/DEBUG: at io.flutter.embedding.engine.FlutterEngineConnectionRegistry.remove(FlutterEngineConnectionRegistry.java:272)
E/DEBUG: at io.flutter.embedding.engine.FlutterEngineConnectionRegistry.remove(FlutterEngineConnectionRegistry.java:282)
E/DEBUG: at io.flutter.embedding.engine.FlutterEngineConnectionRegistry.removeAll(FlutterEngineConnectionRegistry.java:290)
E/DEBUG: at io.flutter.embedding.engine.FlutterEngineConnectionRegistry.destroy(FlutterEngineConnectionRegistry.java:123)
E/DEBUG: at io.flutter.embedding.engine.FlutterEngine.destroy(FlutterEngine.java:460)
E/DEBUG: at id.flutter.flutter_background_service.BackgroundService.onDestroy(BackgroundService.java:121)
E/DEBUG: at android.app.ActivityThread.handleStopService(ActivityThread.java:4986)
问题: 使用flutter_background_service: ^5.0.4 插件进行关闭服务,触发了ImagePickersPlugin 的onDetachedFromEngine 方法,且channel 为空,导致闪退。
闪退报错如下: E/DEBUG: java.lang.RuntimeException: Unable to stop service id.flutter.flutter_background_service.BackgroundService@d8e898a: java.lang.NullPointerException: Attempt to invoke virtual method 'void io.flutter.plugin.common.MethodChannel.setMethodCallHandler(io.flutter.plugin.common.MethodChannel$MethodCallHandler)' on a null object reference E/DEBUG: at android.app.ActivityThread.handleStopService(ActivityThread.java:5006) E/DEBUG: at android.app.ActivityThread.-$$Nest$mhandleStopService(Unknown Source:0) E/DEBUG: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2304) E/DEBUG: at android.os.Handler.dispatchMessage(Handler.java:106) E/DEBUG: at android.os.Looper.loopOnce(Looper.java:211) E/DEBUG: at android.os.Looper.loop(Looper.java:300) E/DEBUG: at android.app.ActivityThread.main(ActivityThread.java:8503) E/DEBUG: at java.lang.reflect.Method.invoke(Native Method) E/DEBUG: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:561) E/DEBUG: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:954) E/DEBUG: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void io.flutter.plugin.common.MethodChannel.setMethodCallHandler(io.flutter.plugin.common.MethodChannel$MethodCallHandler)' on a null object reference E/DEBUG: at com.leeson.image_pickers.ImagePickersPlugin.onDetachedFromEngine(ImagePickersPlugin.java:295) E/DEBUG: at io.flutter.embedding.engine.FlutterEngineConnectionRegistry.remove(FlutterEngineConnectionRegistry.java:272) E/DEBUG: at io.flutter.embedding.engine.FlutterEngineConnectionRegistry.remove(FlutterEngineConnectionRegistry.java:282) E/DEBUG: at io.flutter.embedding.engine.FlutterEngineConnectionRegistry.removeAll(FlutterEngineConnectionRegistry.java:290) E/DEBUG: at io.flutter.embedding.engine.FlutterEngineConnectionRegistry.destroy(FlutterEngineConnectionRegistry.java:123) E/DEBUG: at io.flutter.embedding.engine.FlutterEngine.destroy(FlutterEngine.java:460) E/DEBUG: at id.flutter.flutter_background_service.BackgroundService.onDestroy(BackgroundService.java:121) E/DEBUG: at android.app.ActivityThread.handleStopService(ActivityThread.java:4986)
插件版本: flutter_background_service: ^5.0.4 image_pickers: ^2.0.5
测试手机: 小米 12x Android 13
targetSdkVersion 33
我手动在Android 代码上加上空判断,就不会闪退,建议作者后期更新版本加上空判断: @Override public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) { if(channel!=null){ channel.setMethodCallHandler(null); } }
我从代码打印上看到进入了setup 方法的,具体为什么channel后面为null,还有待验证。