When debugging using Java compilation, the Java file cannot find the DeviceEventManagerModule
I’ve been trying to write a module for react-native that should call a Javascript method when the phone gets a call. But when I run the command react-native run-android
, compileDebugJavaWithJavac
crashes with the following error.
CallListenerModule.java:44 error: package DeviceEventManagerModule does not exist (DeviceEventManagerModule.RCTDeviceEventEmitter.class)
This is the CallListenerModule class:
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 searched the internet for a solution to this problem without success. The sendEvent
method is from copied in docs. I removed @Nullable
from the params parameter because it caused another error and I’m not going to send the event without the parameter.
This is my first article on SO, so any constructive criticism is welcome 🙂
Solution
You forgot to import the class com.facebook.react.modules.core.DeviceEventManagerModule.
Therefore, you can solve your problem by adding the following line:
Import com.facebook.react.modules.core.DeviceEventManagerModule