特此记录一些react-native开发中的一些错误:
在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)
找到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
运行在android中出现的错误
//./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 即可
项目开发中没有启用过蓝牙功能,不知道为啥运行时会debug出来这个错误
错误意思是该组件文本需要使用Text标签包裹起来
//错误
<View>Text</View>
//正确
<View>
<Text>Text</Text>
<View>
#如无特别声明,该文章均为 shufu 原创,转载请遵循 署名-非商业性使用 4.0 国际(CC BY-NC 4.0) 协议,即转载请注明文章来源。
#最后编辑时间为: 2019 年 11 月 16 日