React Native, 'is not usable as a native method argument'

ε祈祈猫儿з 提交于 2019-12-10 13:34:24

问题


I'm new to React Native. While I was developing a native module, I encounter a problem with callbacks. Below is the error message from React Native

02-05 17:43:26.387 32301-32570/com.awesomeproject2 E/ReactNativeJNI: Got JS Exception: Exception calling object as function: abc,function f1() {
                                                                               _ToastExample2.default.show('1111', _ToastExample2.default.SHORT);
                                                                             },72,73 is not usable as a native method argument (<unknown file>:1845)
02-05 17:43:26.387 32301-32570/com.awesomeproject2 E/ReactNativeJNI: Got JS Stack: invariant@http://localhost:8081/index.delta?platform=android&dev=true&minify=false:1838:26
                                                                     enqueueNativeCall@http://localhost:8081/index.delta?platform=android&dev=true&minify=false:2259:20
                                                                     fn@http://localhost:8081/index.delta?platform=android&dev=true&minify=false:1999:40
                                                                     onBtnPress@http://localhost:8081/index.delta?platform=android&dev=true&minify=false:58362:32
                                                                     proxiedMethod@http://localhost:8081/index.delta?platform=android&dev=true&minify=false:34324:37
                                                                     proxiedMethod@[native code]
                                                                     touchableHandlePress@http://localhost:8081/index.delta?platform=android&dev=true&minify=false:37170:47
                                                                     touchableHandlePress@[native code]
                                                                     _performSideEffectsForTransition@http://localhost:8081/index.delta?platform=android&dev=true&minify=false:36821:36
                                                                     _performSideEffectsForTransition@[native code]
                                                                     _receiveSignal@http://localhost:8081/index.delta?platform=android&dev=true&minify=false:36754:46
                                                                     _receiveSignal@[native code]
                                                                     touchableHandleResponderRelease@http://localhost:8081/index.delta?platform=android&dev=true&
02-05 17:43:26.393 32301-32570/com.awesomeproject2 E/unknown:ReactNative: Exception in native call
                                                                          java.lang.RuntimeException: Error calling RCTEventEmitter.receiveTouches
                                                                              at com.facebook.react.bridge.queue.NativeRunnable.run(Native Method)
                                                                              at android.os.Handler.handleCallback(Handler.java:739)
                                                                              at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                              at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:31)
                                                                              at android.os.Looper.loop(Looper.java:135)
                                                                              at com.facebook.react.bridge.queue.MessageQueueThreadImpl$3.run(MessageQueueThreadImpl.java:194)
                                                                              at java.lang.Thread.run(Thread.java:818)
                                                                           Caused by: com.facebook.jni.CppException: Exception calling object as function: abc,function f1() {
                                                                                    _ToastExample2.default.show('1111', _ToastExample2.default.SHORT);
                                                                                  },72,73 is not usable as a native method argument (<unknown file>:1845)
                                                                            ... 7 more

And my native module look like this

@ReactMethod
public void testCallback(String message, Callback c1, Callback c2, Callback c3) {
    if (message.equals("123")) {
        c1.invoke(message);
    } else if (message.equals("abc")) {
        c2.invoke(message);
    } else {
        c3.invoke(message);
    }
}

And my calling function from react

onBtnPress() {

  function f1 () {
    ToastExample.show('1111', ToastExample.SHORT);
  };

  function f2 () {
    ToastExample.show('2222', ToastExample.SHORT);
  };

  function f3 () {
    ToastExample.show('3333', ToastExample.SHORT);
  };

  ToastExample.testCallback('abc', f1, f2, f3);
}

I have no idea that why this error is happening, can someone help me? Thanks.


回答1:


I have the same exception with 3 callbacks. If you use one or two callbacks it works fine

@ReactMethod public void testCallback(String message, Callback c1, Callback c2)

works



来源:https://stackoverflow.com/questions/48619393/react-native-is-not-usable-as-a-native-method-argument

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!