menu
refresh
shufu
这是一个前端开发记录Debug过程和杂谈的博客
react-native开发中出现的各种乱七八糟错误(红屏错误合集)
access_time
brush 233个字
whatshot 84 ℃

react-native开发中出现的各种乱七八糟错误

特此记录一些react-native开发中的一些错误:

1、java.lang.String cannot be cast to java.lang.Boolean

java.lang.String cannot be cast to java.lang.Boolean.png

在ios中运行很正常,在android中运行就出现这种错误

//版本
    "react": "16.8.3",
    "react-native": "0.59.5",

//android studio中调试台打印出:
E/unknown:ReactNative: Exception in native call
    java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Boolean
        at com.facebook.react.bridge.ReadableNativeArray.getBoolean(ReadableNativeArray.java:105)
        at com.facebook.react.bridge.JavaMethodWrapper$1.extractArgument(JavaMethodWrapper.java:37)
        at com.facebook.react.bridge.JavaMethodWrapper$1.extractArgument(JavaMethodWrapper.java:33)
        at com.facebook.react.bridge.JavaMethodWrapper.invoke(JavaMethodWrapper.java:359)
        at com.facebook.react.bridge.JavaModuleWrapper.invoke(JavaModuleWrapper.java:158)
        at com.facebook.react.bridge.queue.NativeRunnable.run(Native Method)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:29)
        at android.os.Looper.loop(Looper.java:214)
        at com.facebook.react.bridge.queue.MessageQueueThreadImpl$4.run(MessageQueueThreadImpl.java:232)
        at java.lang.Thread.run(Thread.java:919)

处理方案:react-native中使用SockJs和StompJs

找到SockJs中的abstract-xhr.js源码

if ((!opts || !opts.noCredentials) && AbstractXHRObject.supportsCORS) {
   debug('withCredentials');
   // Mozilla docs says https://developer.mozilla.org/en/XMLHttpRequest :
   // "This never affects same-site requests."

   this.xhr.withCredentials = 'true';
 }

很明显问题就出在这儿。
查看mozilla官网中对XMLHttpRequest的API描述
The XMLHttpRequest.withCredentials property is a Boolean that indicates whether or not cross-site Access-Control requests should be made using credentials such as cookies, authorization headers or TLS client certificates. Setting withCredentials has no effect on same-site requests.

把SockJs中的"true"改为Boolean类型的true即可

this.xhr.withCredentials = “true”;
//修改为
this.xhr.withCredentials = true;

//建议把stompJS文件和sockJS文件下载放置在资源中即可,如果使用npm的,这样修改可能在多人开发中会出现一些麻烦
debug

2、library not found for -ljcore-ios-2.1.1 - error

错误处理

3、library "libc++_shared.so" not found

运行在android中出现的错误

处理方案:原作者连接:More than one file was found with OS independent path 'lib/x86/libc++_shared.so'

//./android/app/build.gradle
    packagingOptions{
        pickFirst 'lib/armeabi-v7a/libc++_shared.so'
        pickFirst 'lib/x86/libc++_shared.so'
        pickFirst 'lib/arm64-v8a/libc++_shared.so'
        pickFirst 'lib/x86_64/libc++_shared.so'
    }
//解决的办法是将原有的 exclude 替换成 pickFirst 即可

4、E/BluetoothAdapter: Bluetooth binder is null

项目开发中没有启用过蓝牙功能,不知道为啥运行时会debug出来这个错误

5、Invariant Violation: Text strings must be rendered within a <Text> component.

错误意思是该组件文本需要使用Text标签包裹起来

//错误
<View>Text</View>
//正确
<View>
  <Text>Text</Text>
<View>

Simulator Screen Shot - iPhone X - 2019-10-24 at 11.58.13.png

#如无特别声明,该文章均为 shufu 原创,转载请遵循 署名-非商业性使用 4.0 国际(CC BY-NC 4.0) 协议,即转载请注明文章来源。
#最后编辑时间为: 2019 年 11 月 16 日


account_circle
email
explore


DreamCat

主题名称:DreamCat | 版本:X1.6-20201226

主题开发:HanFengA7 | TeddyNight | Dev-Leo | CornWorld

鸣谢:学神之女

鸣谢:学神之女