Flutter升级到V1.20.1之后,FlutterActivity页面关闭崩溃

端木安国
2023-12-01

一、问题描述

(1)问题描述:加载Flutter的页面,当页面关闭时候,App崩溃。

(2)影响版本大概:Flutter Sdk (V1.17.5 - V1.20.1)。

(3)问题原因:disconnectSurfaceFromRenderer在页面关闭释放时候被调用两次,第二次调用时候renderSurface已经为空,造成空指针。

// FlutterRenderer must be non-null.
private void disconnectSurfaceFromRenderer() {
if (flutterRenderer == null) {
throw new IllegalStateException(
"disconnectSurfaceFromRenderer() should only be called when flutterRenderer is non-null.");
}

flutterRenderer.stopRenderingToSurface();
renderSurface.release();
renderSurface = null;

}

(4)  github Issues链接 https://github.com/flutter/flutter/issues/50950

二、崩溃日志

    Process: com.xiaoban.mobile, PID: 8897
    java.lang.RuntimeException: Unable to destroy activity {com.xiaoban.mobile/com.xiaoban.mobile.v4.ui.ActivityFlutter}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.Surface.release()' on a null object reference
        at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:4109)
        at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:4127)
        at android.app.ActivityThread.access$1500(ActivityThread.java:172)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1475)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:145)
        at android.app.ActivityThread.main(ActivityThread.java:5835)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.Surface.release()' on a null object reference
        at io.flutter.embedding.android.FlutterTextureView.disconnectSurfaceFromRenderer(FlutterTextureView.java:224)
        at io.flutter.embedding.android.FlutterTextureView.detachFromRenderer(FlutterTextureView.java:166)
        at io.flutter.embedding.android.FlutterView.detachFromFlutterEngine(FlutterView.java:945)
        at io.flutter.embedding.android.FlutterActivityAndFragmentDelegate.onDestroyView(FlutterActivityAndFragmentDelegate.java:456)
        at io.flutter.embedding.android.FlutterActivity.onDestroy(FlutterActivity.java:577)
        at com.xiaoban.mobile.v4.ui.ActivityFlutter.onDestroy(ActivityFlutter.java:172)
        at android.app.Activity.performDestroy(Activity.java:6451)
        at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1154)
        at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:4096)
        at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:4127) 
        at android.app.ActivityThread.access$1500(ActivityThread.java:172) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1475) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:145) 
        at android.app.ActivityThread.main(ActivityThread.java:5835) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:372) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194) 

三、解决办法

(1)可以等到Flutter Sdk V1.20.1之后再升级

(2)临时解决办法,捕获异常,不让其他崩溃。

    @Override
    protected void onDestroy() {
        try {
            super.onDestroy();
        } catch (Exception e) {
            // known issue of the flutter engine
            // https://github.com/flutter/flutter/issues/58767
            // remove when these changes are in stable engine
            // https://github.com/flutter/engine/pull/19501
        }
    }
 类似资料: