Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

没有执行预加载的代码会报错 #86

Open
moujunqiang opened this issue Aug 3, 2020 · 3 comments
Open

没有执行预加载的代码会报错 #86

moujunqiang opened this issue Aug 3, 2020 · 3 comments

Comments

@moujunqiang
Copy link

No description provided.

@moujunqiang
Copy link
Author

ReactInstanceManager reactInstanceManager = ((ReactApplication)getApplication()).getReactNativeHost().getReactInstanceManager();
if (!reactInstanceManager.hasStartedCreatingInitialContext()) {
reactInstanceManager.createReactContextInBackground();//这里会先加载基础包platform.android.bundle,也可以不加载
}
如果注释掉MainActivity这段代码会报错

@hexiangyuan
Copy link

hexiangyuan commented Aug 20, 2020

如果注释掉MainActivity中的这几行

ReactInstanceManager reactInstanceManager = ((ReactApplication)getApplication()).getReactNativeHost().getReactInstanceManager();
        if (!reactInstanceManager.hasStartedCreatingInitialContext()) {
            reactInstanceManager.createReactContextInBackground();
        }

会导致崩溃

修复方法是:
修改AsyncReactActivity中

//默认給null的ComponentName
  protected ReactActivityDelegate createReactActivityDelegate() {
        return new ReactActivityDelegate(this, null);
    }


//runApp中加载完成了Bundle再调用mDelegate.loadApp(getMainComponentName());
protected void runApp(String scriptPath){
        if(scriptPath!=null){
            scriptPath = "file://"+scriptPath.substring(0,scriptPath.lastIndexOf(File.separator)+1);
        }
        final String path = scriptPath;
        final RnBundle bundle = getBundle();
        final ReactInstanceManager reactInstanceManager = ((ReactApplication)getApplication()).getReactNativeHost().getReactInstanceManager();
        if(bundle.scriptType == ScriptType.NETWORK){//如果是网络加载的话,此时正在子线程
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    ScriptLoadUtil.setJsBundleAssetPath(
                            reactInstanceManager.getCurrentReactContext(),
                            path);
                    mDelegate.loadApp(getMainComponentNameInner());
                }
            });
        } else {//主线程运行
            ScriptLoadUtil.setJsBundleAssetPath(
                    reactInstanceManager.getCurrentReactContext(),
                    path);
            mDelegate.loadApp(getMainComponentName());
        }
    }

@hexiangyuan
Copy link

可以参考
#88

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants