空对象上的 getApplicationContext(),React Native Android 模块
目前正在使用 java SDK 为我的 React Native 应用程序创建一个原生 Android 模块。我尝试启动需要上下文作为参数的 SDK。代码如下:
public class LcrSdkModule extends ReactContextBaseJavaModule {
Context context;
LcrSdkModule(ReactApplicationContext ReactContext) {
super(ReactContext);
this.context = ReactContext.getApplicationContext(); // This is where you get the context
}
@Override
public String getName() {
return "LcrSdkModule";
}
@ReactMethod
public int printSomething(Integer a, Integer b) {
return a + b;
}
private LcrSdk lcrSdk = new LcrSdk(this.context);
}
“private LcrSdk lcrSdk = new LcrSdk(this.context);”如果我没有记错的话,这就是代码失败的地方。这是我在 React Native Metro 端收到的错误:
Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object
这里有什么建议吗?似乎 ReactContext 为空。
Currently creating a native android module for my react native application using a java SDK. I try to initiate the SDK which needs a context as a parameter. Here is the code:
public class LcrSdkModule extends ReactContextBaseJavaModule {
Context context;
LcrSdkModule(ReactApplicationContext ReactContext) {
super(ReactContext);
this.context = ReactContext.getApplicationContext(); // This is where you get the context
}
@Override
public String getName() {
return "LcrSdkModule";
}
@ReactMethod
public int printSomething(Integer a, Integer b) {
return a + b;
}
private LcrSdk lcrSdk = new LcrSdk(this.context);
}
The "private LcrSdk lcrSdk = new LcrSdk(this.context);" is where the code is failing if I'm not mistaken. Here is the error I'm receiving on the react native metro side:
Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object
Any advice here? Just seems like ReactContext is null.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论