我正在寻找在单个程序包中的活动和服务上实现运行本机应用程序的方法。
经常在活动中运行RN应用程序,但是找不到在该服务上执行另一个React Native应用程序的正确方法。后者应在前台服务上运行,并在窗口管理器上查看。
我的代码大致如下:
// ReactNativeWindowService.kt
fun startNewReactWindow(
reactContext: ReactContext,
jsMainModuleName: String?,
bundleAssetName: String?,
module: String
) {
Toast.makeText(reactContext, "START()", Toast.LENGTH_SHORT).show()
val headLayout = createHeadLayout(reactContext)
headsManager.addLayout(headLayout)
launch(UI) {
reactInstanceManager = createReactInstanceManager(reactContext, jsMainModuleName, bundleAssetName)
reactRootView = ReactRootView(reactContext)
reactRootView?.startReactApplication(reactInstanceManager, module, null)
headsManager.addHeadView(bubbleLayout.apply { addView(reactRootView) })
}
}
但是,这段代码会产生:
07-10 01:05:07.662 26269-26380/Sample E/ReactNativeJS: Error while starting app: TypeError: undefined is not an object (evaluating 'NativeReactModule.startApp')
07-10 01:05:07.674 26269-26380/Sample I/ReactNativeJS: Running application "index" with appParams: {"rootTag":21}. __DEV__ === true, development-level warning are ON, performance optimizations are OFF
07-10 01:05:07.674 26269-26380/Sample E/ReactNativeJS: Application index has not been registered.
Hint: This error often happens when you're running the packager (local dev server) from a wrong folder. For example you have multiple apps and the packager is still running for the app you were working on before.
If this is the case, simply kill the old packager instance (e.g. close the packager terminal window) and start the packager in the correct app folder (e.g. cd into app folder and run 'npm start').
This error can also happen due to a require() error during initialization or failure to call AppRegistry.registerComponent.
我确定代码正确地传递了诸如jsMainModuleName之类的参数来创建ReactInstanceManager。 (我的参数与活动的参数相同)
我在代码中遗漏了什么?或任何其他对我有帮助的参考/文档/建议。提前致谢。 :)
答案 0 :(得分:0)
您必须确保您的文件MainAcitivity.java具有功能
protected String getMainComponentName() {
return "your file name for example abc";
}
但是在index.android.js文件中,您注册了其他组件
AppRegistry.registerComponent('123', () => 123);
必须在什么时候
AppRegistry.registerComponent('abc', () => abc);
尝试修复它。