尝试在空对象引用上调用接口方法 java.lang.String com.facebook.react.bridge.ReadableMap 等

Attemp to invoke interface method java.lang.String com.facebook.react.bridge.ReadableMap etc on a null object reference

我在将图像上传到 Firebase 存储后收到此错误。我在用 "react-native": "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 上调用接口方法 'java.lang.String com.facebook.react.bridge.ReadableMap.getString(java.lang.String)' FileReaderModule.java:43 调用 Method.java 调用 JavaMethodWrapper.java:372 调用 JavaModuleWrapper.java:160 运行 NativeRunnable.java handleCallback Handler.java:790 dispatchMessage Handler.java:99 dispatchMessage MessageQueueThreadHandler.java:29 loop Looper.java:164 运行 MessageQueueThreadImpl.java:192 运行 Thread.java:764

我 运行 遇到了同样的问题。它与准备语句有关:

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

如果我把它们注释掉就不会出现这个错误

我遇到了同样的错误。解决方案是按照官方文档的说明执行 'Fetch replacement':

Since we are not implementing FileReader polyfill, you might run into this error in some cases.

If you're facing this problem with Blob polyfill in Debug Mode, try replace window.fetch with fetch replacement should fix it.

并且:

If you have existing code that uses whatwg-fetch, now you don't have to change existing code after 0.9.0, just use fetch replacement. The difference between Official fetch and fetch replacement is that, official fetch uses WHATWG-fetch js library which wraps XMLHttpRequest polyfill under the hood, and our implementation simply wraps 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

我已经通过删除所有这些包解决了这个问题,因为即使使用 fetch 替换,错误仍然出现我认为是由 window.XMLHttpRequest = RNFetchBlob.polyfill.XMLHttpRequest; 所以我用了老办法

const uriToBlob = (uri) => {
    return new Promise((resolve, reject) => {
        const xhr = new XMLHttpRequest();
        xhr.onload = function () {
            resolve(xhr.response);
        };

        xhr.onerror = function () {
            reject(new Error('uriToBlob failed'));
        };
        xhr.responseType = 'blob';
        xhr.open('GET', uri, true);

        xhr.send(null);
    });
}