[英]Java file can't find DeviceEventManagerModule when compiling debug with Java
I have been trying to write a module for react-native thats should call a Javascript method when the phone recevies a call. 我一直在尝试为react-native编写一个模块,当手机收到一个电话时,应该调用一个Javascript方法。 But when i run the command
react-native run-android
the compileDebugJavaWithJavac
craches with the following error. 但是当我运行命令
react-native run-android
, compileDebugJavaWithJavac
会出现以下错误。
CallListenerModule.java:44 error: package DeviceEventManagerModule does not exist (DeviceEventManagerModule.RCTDeviceEventEmitter.class)
this is the CallListenerModule class: 这是CallListenerModule类:
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.bridge.Arguments;
import android.util.Log;
public class CallListenerModule extends ReactContextBaseJavaModule {
BroadcastReceiverCustom broadcastRecevier;
ReactContext context;
public CallListenerModule(ReactApplicationContext reactContext) {
super(reactContext);
context = reactContext;
broadcastRecevier = new BroadcastReceiverCustom(reactContext);
}
@Override
public String getName() {
return "CallListenerModule";
}
public void sendCallEvent(String incomingNumber){
WritableMap params = Arguments.createMap();
params.putString("Number", incomingNumber);
sendEvent(context, "CallRecevied", params);
}
private void sendEvent(ReactContext reactContext,
String eventName,
WritableMap params) {
reactContext
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit(eventName, params);
}
}
I have search the great internet for a solution for this problem but with no luck. 我已经在互联网上寻找解决这个问题的方法,但没有运气。 The
sendEvent
method is copied from the docs . sendEvent
方法是从文档中复制的。 I removed the @Nullable
from the params parameter because it casued another error and I do not intend on sending event without a parameter. 我从params参数中删除了
@Nullable
,因为它引发了另一个错误,我不打算在没有参数的情况下发送事件。
This is my first post on SO so any constructive criticism is appreciated :) 这是我关于SO的第一篇文章,所以任何建设性的批评都赞赏:)
You forgot to import the class com.facebook.react.modules.core.DeviceEventManagerModule
. 您忘记导入类
com.facebook.react.modules.core.DeviceEventManagerModule
。 Therefore you can solve your problem by adding the following line: 因此,您可以通过添加以下行来解决您的问题:
import com.facebook.react.modules.core.DeviceEventManagerModule
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.