wuba/magpie_sdk

[健壮性]当在Acitivty中初始化sdk时,启动flutter页面即崩溃

avenwu opened this issue · 0 comments

根据README指引,直接copy代码后接入,出现崩溃,代码如下:

public class XXXActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.xxxx);
        MagpiePlatform platform = new Magpie
                .MagpieConfigBuilder(getApplication(), new MagpieRouter())
                .isDebug(true)
                .whenEngineStart(FlutterBoost.ConfigBuilder.FLUTTER_ACTIVITY_CREATED)
                .renderMode(FlutterView.RenderMode.texture)
                .lifecycleListener(new Magpie.MagpieLifecycleListener() {

                    @Override
                    public void onEngineCreated() {

                    }

                    @Override
                    public void onPluginsRegistered() {

                    }

                    @Override
                    public void onEngineDestroy() {

                    }
                })
                .build();
        Magpie.getInstance().init(platform);
        findViewById(R.id.tv_text).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PageRouter.openPageByUrl(TraceReportActivity.this, "trace://report", null);
            }
        });

    }


    public static class MagpieRouter implements IMagpienativeRouter {

        @Override
        public void openContainer(Context context, String url, Map<String, Object> urlParams,
                                  int requestCode, Map<String, Object> exts) {
            String assembleUrl = Utils.assembleUrl(url, urlParams);
            PageRouter.openPageByUrl(context, assembleUrl, (HashMap) urlParams);
        }
    }
}

崩溃堆栈

2020-06-04 15:59:45.624 9214-9214/cn.hacktons.myapplication D/AndroidRuntime: Shutting down VM
2020-06-04 15:59:45.628 9214-9214/cn.hacktons.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
    Process: cn.hacktons.myapplication, PID: 9214
    java.lang.RuntimeException: Unable to resume activity {cn.hacktons.myapplication/com.idlefish.flutterboost.containers.BoostFlutterActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'com.idlefish.flutterboost.BoostPluginRegistry$BoostRegistrarAggregate com.idlefish.flutterboost.BoostPluginRegistry.getRegistrarAggregate()' on a null object reference
        at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4341)
        at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4373)
        at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:52)
        at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2043)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:216)
        at android.app.ActivityThread.main(ActivityThread.java:7464)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:549)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:955)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.idlefish.flutterboost.BoostPluginRegistry$BoostRegistrarAggregate com.idlefish.flutterboost.BoostPluginRegistry.getRegistrarAggregate()' on a null object reference
        at com.idlefish.flutterboost.containers.FlutterActivityAndFragmentDelegate.onResume(FlutterActivityAndFragmentDelegate.java:180)
        at com.idlefish.flutterboost.containers.BoostFlutterActivity.onResume(BoostFlutterActivity.java:258)
        at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1456)
        at android.app.Activity.performResume(Activity.java:8125)
        at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4331)
        at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4373) 
        at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:52) 
        at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2043) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:216) 
        at android.app.ActivityThread.main(ActivityThread.java:7464) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:549) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:955) 

问题分析:
通过分析Boost崩溃源码,registry为空,正常情况初始化boost应当会进行赋值,实际没有。

        BoostPluginRegistry registry = (BoostPluginRegistry) FlutterBoost.instance().getPluginRegistry();

规避办法:
将 .whenEngineStart(FlutterBoost.ConfigBuilder.FLUTTER_ACTIVITY_CREATED)修改为.whenEngineStart(FlutterBoost.ConfigBuilder.IMMEDIATELY)