【问题标题】:Attemp to invoke interface method java.lang.String com.facebook.react.bridge.ReadableMap ... on a null object reference尝试在空对象引用上调用接口方法 java.lang.String com.facebook.react.bridge.ReadableMap ...
【发布时间】:2018-11-20 00:49:31
【问题描述】:

将图片上传到 Firebase 存储后,我收到此错误。我在用 “反应原生”:“0.55.4”, "react-native-fetch-blob": "^0.10.8", "react-native-image-picker": "^0.26.10", "firebase": "^5.0.4",

这是我上传图片的代码。

// Prepare Blob support
const Blob = RNFetchBlob.polyfill.Blob;

const fs = RNFetchBlob.fs;
window.XMLHttpRequest = RNFetchBlob.polyfill.XMLHttpRequest;
window.Blob = Blob;

uploadImage = (uri, imageName, mime = "image/jpg") => {
    return new Promise((resolve, reject) => {
      const uploadUri =
        Platform.OS === "ios" ? uri.replace("file://", "") : uri;
      let uploadBlob = null;
      const imageRef = db
        .storage()
        .ref("images/")
        .child(imageName);

  fs.readFile(uploadUri, "base64")
    .then(data => {
      return Blob.build(data, { type: `${mime};BASE64` });
    })
    .then(blob => {
      uploadBlob = blob;
      alert("blob is " + JSON.stringify(blob));
      return imageRef.put(blob, { contentType: mime });
    })
    .then(() => {
      uploadBlob.close();
      return imageRef.getDownloadURL();
    })
    .then(url => {
      resolve(url);
    })
    .catch(error => {
      reject(error);
    });
});};

尝试在空对象引用 readAsText FileReaderModule.java:43 调用 Method.java 调用 JavaMethodWrapper 上调用接口方法“java.lang.String com.facebook.react.bridge.ReadableMap.getString(java.lang.String)” .java:372 调用 JavaModuleWrapper.java:160 运行 NativeRunnable.java handleCallback Handler.java:790 dispatchMessage Handler.java:99 dispatchMessage MessageQueueThreadHandler.java:29 循环 Looper.java:164 运行 MessageQueueThreadImpl.java:192 运行 Thread.java:764

【问题讨论】:

  • 请将完整的错误添加到问题中

标签: android reactjs firebase react-native storage


【解决方案1】:

我遇到了同样的错误。解决方案是按照官方文档的说明进行“获取替换”:

由于我们没有实现 FileReader polyfill,你可能会遇到 在某些情况下会出现此错误。

如果您在调试模式下使用 Blob polyfill 遇到此问题,请尝试 用 fetch 替换替换 window.fetch 应该可以解决它。

还有:

如果您有使用 whatwg-fetch 的现有代码,现在您没有 要在 0.9.0 之后更改现有代码,只需使用 fetch 替换。这 Official fetch 和 fetch replacement 的区别在于, 官方 fetch 使用包装 XMLHttpRequest 的 WHATWG-fetch js 库 引擎盖下的 polyfill,我们的实现简单地包装了 RNFetchBlob.fetch.

基本上,您只需将其添加到您的代码中,就在您的 window.Blob = Blob; 行下方:

const Fetch = RNFetchBlob.polyfill.Fetch
// replace built-in fetch
window.fetch = new Fetch({
    // enable this option so that the response data conversion handled automatically
    auto : true,
    // when receiving response data, the module will match its Content-Type header
    // with strings in this array. If it contains any one of string in this array, 
    // the response body will be considered as binary data and the data will be stored
    // in file system instead of in memory.
    // By default, it only store response data to file system when Content-Type 
    // contains string `application/octet`.
    binaryContentTypes : [
        'image/',
        'video/',
        'audio/',
        'foo/',
    ]
}).build()

文档: https://github.com/wkh237/react-native-fetch-blob/wiki/Trouble-Shooting#failed-to-execute-readastext-on-filereader

【讨论】:

  • 欢迎提供解决方案链接,但请确保您的答案在没有它的情况下有用:add context around the link 这样您的其他用户就会知道它是什么以及为什么会出现,然后引用最相关的您链接到的页面的一部分,以防目标页面不可用。 Answers that are little more than a link may be deleted.
  • 嗨,Paul,我是 StackOverflow 的新手,非常感谢您的反馈。我将编辑我的答案并将其考虑到未来的答案中。
猜你喜欢
  • 2018-11-21
  • 2015-11-13
  • 2016-04-23
  • 1970-01-01
  • 1970-01-01
  • 2018-08-25
  • 2017-09-01
  • 1970-01-01
相关资源
最近更新 更多