Open moujunqiang opened 4 years ago
如果注释掉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());
}
}
ReactInstanceManager reactInstanceManager = ((ReactApplication)getApplication()).getReactNativeHost().getReactInstanceManager(); if (!reactInstanceManager.hasStartedCreatingInitialContext()) { reactInstanceManager.createReactContextInBackground();//这里会先加载基础包platform.android.bundle,也可以不加载 } 如果注释掉MainActivity这段代码会报错