Android undefined不是评估自定义模块的对象 - React Native

时间:2017-08-05 21:49:38

标签: android react-native npm

我正在尝试学习如何编写反应原生模块,并且我遇到了这个错误:

enter image description here

以下是我的代码:

Module.java(../ android / src / main / java / com / reactlibrary)

public class RNGifMakerModule extends ReactContextBaseJavaModule {

  private final ReactApplicationContext reactContext;

  public RNGifMakerModule(ReactApplicationContext reactContext) {
    super(reactContext);
    this.reactContext = reactContext;
  }

  @Override
  public String getName() {
    return "RNGifMaker";
  }

  @ReactMethod
  public void alert(String message) {
    Toast.makeText(getReactApplicationContext(), message, Toast.LENGTH_LONG).show();
  }
}

index.android.js

import { RNGifMaker } from 'GifMaker';

const onButtonPress = () => {
 RNGifMaker.alert('Hello World');
};

的package.json

"dependencies": {
    "react": "16.0.0-alpha.6",
    "react-native": "0.43.3",
    "GifMaker":"file:../"
},

index.js(../)

import { NativeModules } from 'react-native';

module.exports = NativeModules.RNGifMaker;

我同时运行npm installreact-native link

我(大部分时间)都跟着this tutorial

1 个答案:

答案 0 :(得分:2)

我明白了!在Module.java我回到了错误的班级。

当时:

public String getName() {
    return "RNGifMaker";
}

应该是:

 public String getName() {
    return "RNGifMakerModule";
  }