React Native,'不能用作本机方法参数'

时间:2018-02-05 09:32:51

标签: javascript java android react-native-android

我是React Native的新手。在我开发本机模块时,我遇到了回调问题。 以下是来自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

我的原生模块看起来像这样

@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);
    }
}

我的召唤函数来自

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);
}

我不知道为什么会发生这种错误,有人可以帮助我吗?感谢。

4 个答案:

答案 0 :(得分:0)

当您向属性传递不合适的内容时,就会出现这种错误。例如,我没有将确切的格式传递给 Image URI 属性,因为它会引发错误。此错误在很大程度上表明了其自身。

没错。

<Image source = {{ uri: 'https://reactnative.dev/img/tiny_logo.png'}}>

但是当你做这样的事情时。

<Image source = {{ uri: '<NaN'}}>
<块引用>

大多数情况下,当您从 API 获取错误的数据格式时会发生这种情况。

<块引用>

我的回答不适合这个特定的问题,而是要找到 确切的标题,我已经多次登陆这个链接,所以为了 节省其他人的时间,我想在这里发布这个答案。

答案 1 :(得分:0)

我在使用带有 formData 字段的 Axios 时遇到此问题,但未将附加数据转换为字符串格式。

给出错误:

const formData = new FormData();
formData.append(<field_name>, field_data);

必须采用以下形式:

const formData = new FormData();
formData.append(<field_name>, JSON.stringify(field_data));

答案 2 :(得分:-1)

我有3个回调相同的例外。如果你使用一个或两个回调它可以正常工作

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

作品

答案 3 :(得分:-1)

我也收到了“ is not usable as a native method argument”错误,这是因为我注释和/或删除了组件的render()方法,例如以下示例:

import React from 'react';

import Reinput from 'reinput'

export default class TextField extends Reinput {
  //render() {
  //  return (
  //    <>
  //      {super.render()}
  //    </>
  //  );
  //}
}
  

注意:我使用继承基于现有组件创建了一个子类,并且我认为当我不重写基类的render()方法时,它将被使用。 (当时我还不知道我们可以使用<></>

产生的错误日志:

Invariant Violation: Invariant Violation: [25,"AndroidTextInput",71,{"autoCapitalize":16384,"allowFontScaling":true,"onContentSizeChange":true,"onSelectionChange":true,"onScroll":true,"color":-65536,"height":"<<NaN>>","textAlignVertical":"top","underlineColorAndroid":0,"mostRecentEventCount":0,"text":""}] is not usable as a native method argument

This error is located at:
    in AndroidTextInput (at TextInput.js:1193)
    in TouchableWithoutFeedback (at TextInput.js:1211)
    in TextInput (at Input.js:82)
    in RCTView (at View.js:45)
    in View (at Input.js:76)
    in RCTView (at View.js:45)
    in View (at Input.js:75)
    in RCTView (at View.js:45)
    in View (at Input.js:73)
    in ReinputInput (at login.view.tsx:42)
    in RCTView (at View.js:45)
    in View (at login.view.tsx:32)
    in LoginView (at SceneView.js:9)
    in SceneView (at StackViewLayout.js:786)
    in RCTView (at View.js:45)
    in View (at StackViewLayout.js:785)
    in RCTView (at View.js:45)
    in View (at StackViewLayout.js:784)
    in RCTView (at View.js:45)
    in View (at createAnimatedComponent.js:151)
    in AnimatedComponent (at StackViewCard.js:69)
    in RCTView (at View.js:45)
    in View (at createAnimatedComponent.js:151)
    in AnimatedComponent (at screens.native.js:59)
    in Screen (at StackViewCard.js:57)
    in Card (at createPointerEventsContainer.js:27)
    in Container (at StackViewLayout.js:862)
    in RCTView (at View.js:45)
    in View (at screens.native.js:83)
    in ScreenContainer (at StackViewLayout.js:313)
    in RCTView (at View.js:45)
    in View (at createAnimatedComponent.js:151)
    in AnimatedComponent (at StackViewLayout.js:309)
    in PanGestureHandler (at StackViewLayout.js:302)
    in StackViewLayout (at withOrientation.js:30)
    in withOrientation (at StackView.js:79)
    in RCTView (at View.js:45)
    in View (at Transitioner.js:214)
    in Transitioner (at StackView.js:22)
    in StackView (at createNavigator.js:61)
    in Navigator (at createKeyboardAwareNavigator.js:12)
    in KeyboardAwareNavigator (at createAppContainer.js:387)
    in NavigationContainer (at App.tsx:42)
    in App (at renderApplication.js:35)
    in RCTView (at View.js:45)
    in View (at AppContainer.js:98)
    in RCTView (at View.js:45)
    in View (at AppContainer.js:115)
    in AppContainer (at renderApplication.js:34)

This error is located at:
    in NavigationContainer (at App.tsx:42)
    in App (at renderApplication.js:35)
    in RCTView (at View.js:45)
    in View (at AppContainer.js:98)
    in RCTView (at View.js:45)
    in View (at AppContainer.js:115)
    in AppContainer (at renderApplication.js:34)
enqueueNativeCall
    D:\my-app\node_modules\react-native\Libraries\ART\ReactNativeART.js:292:16
fn
    D:\my-app\node_modules\react-native\Libraries\Components\ActivityIndicator\ActivityIndicator.js:111
createInstance
    D:\my-app\node_modules\react-native\Libraries\Lists\VirtualizedList.js:666:4
completeWork
    D:\my-app\node_modules\react\cjs\react.development.js:624:26
completeUnitOfWork
    D:\my-app\node_modules\prop-types\factoryWithTypeCheckers.js:162:34
performUnitOfWork
    D:\my-app\node_modules\prop-types\factoryWithTypeCheckers.js:325:9
workLoop
    D:\my-app\node_modules\prop-types\factoryWithTypeCheckers.js:333:8
renderRoot

performWorkOnRoot
    D:\my-app\node_modules\react-native\Libraries\Utilities\MatrixMath.js:142:21
performWork
    D:\my-app\node_modules\react-native\Libraries\Utilities\MatrixMath.js:47:27
performSyncWork
    D:\my-app\node_modules\react-native\Libraries\Utilities\deprecatedPropType.js:22:68
requestWork

scheduleWork

scheduleRootUpdate
    D:\my-app\node_modules\react-native\Libraries\Utilities\MatrixMath.js:537:16
render
    D:\my-app\node_modules\react-native\Libraries\Renderer\oss\ReactNativeRenderer-dev.js:737:20
renderApplication
    D:\my-app\node_modules\react-native\Libraries\Components\Picker\PickerAndroid.android.js:106:11
run

runApplication
    D:\my-app\node_modules\react-native\Libraries\YellowBox\Data\YellowBoxWarning.js:56:4
__callFunction
    D:\my-app\node_modules\react-native\Libraries\ART\ReactNativeART.js:389:6
<unknown>
    D:\my-app\node_modules\react-native\Libraries\ART\ReactNativeART.js:156:4
__guard
    D:\my-app\node_modules\react-native\Libraries\ART\ReactNativeART.js:342:13
callFunctionReturnFlushedQueue
    D:\my-app\node_modules\react-native\Libraries\ART\ReactNativeART.js:157:6
callFunctionReturnFlushedQueue
    [native code]