我正在尝试创建Ken Wheeler应用浏览器的android实现,以进行本机响应。 https://github.com/kenwheeler/app-browser 我相信我需要从UI组件返回一个新的React根目录,但是会收到错误消息
package com.inceptionhost.rctframe;
import android.app.Activity;
import android.app.Application;
import android.content.Context;
import com.facebook.react.bridge.ReactApplicationContext;
import
com.facebook.react.modules.core.DefaultHardwareBackBtnHandler;
import com.facebook.react.uimanager.SimpleViewManager;
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.annotations.ReactProp;
import com.facebook.react.common.LifecycleState;
import com.facebook.react.ReactApplication;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.*;
import com.facebook.react.ReactInstanceManager;
import com.facebook.react.ReactRootView;
import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactPackage;
import com.facebook.react.shell.MainReactPackage;
public class RCTFrameManager extends SimpleViewManager<ReactRootView> {
public static final String REACT_CLASS = "RCTFrame";
public String _bundlePath;
RCTFrameManager(){
String _bundlePath = "";
}
@Override
public String getName() {
return REACT_CLASS;
}
@Override
public ReactRootView createViewInstance(
ThemedReactContext context){
ReactInstanceManager mReactInstanceManager;
Application app = new Application();
Context appctx = app.getBaseContext();
ReactApplicationContext RCT_CTX = new ReactApplicationContext(appctx);
mReactInstanceManager = ReactInstanceManager
.builder()
.setApplication(app)
.setCurrentActivity(new Activity())
.setDefaultHardwareBackBtnHandler(new
DefaultHardwareBackBtnHandler() {
@Override
public void invokeDefaultOnBackPressed() {
}
})
.setJSBundleFile(_bundlePath)// or
////.setJSMainModulePath("")
.build();
mReactInstanceManager
.getDevSupportManager()
.setDevSupportEnabled(false);
mReactInstanceManager.createReactContextInBackground();
ReactRootView _rootView = new ReactRootView(context);
_rootView.startReactApplication(
mReactInstanceManager,
"ExampleApp");
return _rootView;
//maybe something like this?
//ReactRootView mReactRootView = new ReactRootView(app.getApplicationContext());
//mReactInstanceManager = ReactInstanceManager.builder()
// .setApplication(app)//getApplication()
// .setJSBundleFile(_bundlePath)
// .addPackage(new MainReactPackage())
// .setUseDeveloperSupport(BuildConfig.DEBUG)
// .setInitialLifecycleState(LifecycleState.RESUMED)
// .build();
// The string here (e.g. "MyReactNativeApp") has to match
// the string in AppRegistry.registerComponent() in index.js
//mReactRootView.startReactApplication(
// mReactInstanceManager,
// "ExampleApp",
// null
//);
//return mReactRootView;
}
@ReactProp(name = "bundle")
public void setBundle(ReactRootView view, String prop) {
_bundlePath = prop;
}
}
预期:给定的包将被加载到反应根视图中,该根视图将显示在当前反应视图树中。
实际:在logcat中出现带有此错误的红色框:
"Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
at com.facebook.react.bridge.ReactApplicationContext.<init>(ReactApplicationContext.java:21)
at com.inceptionhost.rctframe.RCTFrameManager.createViewInstance(RCTFrameManager.java:61)
at com.inceptionhost.rctframe.RCTFrameManager.createViewInstance(RCTFrameManager.java:29)
at com.facebook.react.uimanager.ViewManager.createView(ViewManager.java:42)
at com.facebook.react.uimanager.NativeViewHierarchyManager.createView(NativeViewHierarchyManager.java:260)
at com.facebook.react.uimanager.UIViewOperationQueue$CreateViewOperation.execute(UIViewOperationQueue.java:200)
at com.facebook.react.uimanager.UIViewOperationQueue$1.run(UIViewOperationQueue.java:888)
at com.facebook.react.uimanager.UIViewOperationQueue.flushPendingBatches(UIViewOperationQueue.java:1001)
at com.facebook.react.uimanager.UIViewOperationQueue.access$2400(UIViewOperationQueue.java:46)
at com.facebook.react.uimanager.UIViewOperationQueue$DispatchUIFrameCallback.doFrameGuarded(UIViewOperationQueue.java:1061)
at com.facebook.react.uimanager.GuardedFrameCallback.doFrame(GuardedFrameCallback.java:29)
at com.facebook.react.modules.core.ReactChoreographer$ReactChoreographerDispatcher.doFrame(ReactChoreographer.java:134)
at com.facebook.react.modules.core.ChoreographerCompat$FrameCallback$1.doFrame(ChoreographerCompat.java:105)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:909)
at android.view.Choreographer.doCallbacks(Choreographer.java:723)
at android.view.Choreographer.doFrame(Choreographer.java:655)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)"