简体   繁体   中英

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. But when i run the command react-native run-android the compileDebugJavaWithJavac craches 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 have search the great internet for a solution for this problem but with no luck. The sendEvent method is copied from the docs . I removed the @Nullable from the params parameter because it casued another error and I do not intend on sending event without a parameter.

This is my first post on SO so any constructive criticism is appreciated :)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM