如何修复 RN 中的`cannot be cast to java.lang.String`?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50725705/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 23:47:10  来源:igfitidea点击:

How to fix `cannot be cast to java.lang.String` in RN?

javaandroidreact-native

提问by DNB5brims

I got this error in my RN application in Android:

我在 Android 的 RN 应用程序中遇到此错误:

06-06 16:37:54.455 7506-7569/system_process E/AudioTrack: Could not get audio output for session 729, stream type -1, usage 13, sample rate 48000, format 0x1, channel mask 0x3, flags 0x4 06-06 16:37:54.455 7506-7569/system_process E/SoundPool: Error creating AudioTrack 06-06 16:37:54.924 1286-1286/? E/EGL_emulation: tid 1286: eglCreateSyncKHR(1669): error 0x3004 (EGL_BAD_ATTRIBUTE) 06-06 16:37:55.190 11821-11894/com.efiat_rn E/unknown:ReactNative: Exception in native call java.lang.ClassCastException: com.facebook.react.bridge.ReadableNativeMap cannot be cast to java.lang.String at com.facebook.react.bridge.ReadableNativeMap.getString(ReadableNativeMap.java:168) at com.facebook.react.modules.dialog.DialogModule.showAlert(DialogModule.java:232) at java.lang.reflect.Method.invoke(Native Method) at com.facebook.react.bridge.JavaMethodWrapper.invoke(JavaMethodWrapper.java:372) at com.facebook.react.bridge.JavaModuleWrapper.invoke(JavaModuleWrapper.java:160) at com.facebook.react.bridge.queue.NativeRunnable.run(Native Method) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:29) at android.os.Looper.loop(Looper.java:154) at com.facebook.react.bridge.queue.MessageQueueThreadImpl$3.run(MessageQueueThreadImpl.java:192) at java.lang.Thread.run(Thread.java:761) 06-06 16:37:55.191 11821-11894/com.efiat_rn E/unknown:ReactNative: Exception in native call java.lang.ClassCastException: com.facebook.react.bridge.ReadableNativeMap cannot be cast to java.lang.String at com.facebook.react.bridge.ReadableNativeMap.getString(ReadableNativeMap.java:168) at com.facebook.react.modules.dialog.DialogModule.showAlert(DialogModule.java:232) at java.lang.reflect.Method.invoke(Native Method) at com.facebook.react.bridge.JavaMethodWrapper.invoke(JavaMethodWrapper.java:372) at com.facebook.react.bridge.JavaModuleWrapper.invoke(JavaModuleWrapper.java:160) at com.facebook.react.bridge.queue.NativeRunnable.run(Native Method) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:29) at android.os.Looper.loop(Looper.java:154) at com.facebook.react.bridge.queue.MessageQueueThreadImpl$3.run(MessageQueueThreadImpl.java:192) at java.lang.Thread.run(Thread.java:761)

06-06 16:37:54.455 7506-7569/system_process E/AudioTrack:无法获得会话 729 的音频输出,流类型 -1,用法 13,采样率 48000,格式 0x1,通道掩码 0x3,标志 0x4 06-06 16:37:54.455 7506-7569/system_process E/SoundPool:创建 AudioTrack 06-06 16:37:54.924 1286-1286/? E/EGL_emulation: tid 1286: eglCreateSyncKHR(1669): error 0x3004 (EGL_BAD_ATTRIBUTE) 06-06 16:37:55.190 11821-11894/com.efiat_rn E/unknown:Exception in Native call.Exception facebook.react.bridge.ReadableNativeMap 无法转换为 java.lang.String com.facebook.react.bridge.ReadableNativeMap.getString(ReadableNativeMap.java:168) com.facebook.react.modules.dialog.DialogModule.showAlert( DialogModule.java:232) 在 java.lang.reflect.Method。

It runs perfectly fine on iOS version. It don't show me which JS I do wrong. and I have no clue or ideas on how to fix it. It prompts when I launch the application, when I dismiss the error message, I can see a half-load application, but not clickable. Is there any ideas on how can I start the debugging? Thanks.

它在 iOS 版本上运行得非常好。它没有告诉我我做错了哪个 JS。我对如何解决它没有任何线索或想法。当我启动应用程序时它会提示,当我关闭错误消息时,我可以看到一个半加载的应用程序,但无法点击。关于如何开始调试有什么想法吗?谢谢。

回答by Sujith Niraikulathan

In ReactNative you are calling a method of the native bridge which requires String parameters. But what you are actually sending is JsonObject.

在 ReactNative 中,您正在调用需要字符串参数的本机桥接方法。但你实际发送的是 JsonObject。

Example,

例子,

const data = {message: "hello"};   // Consider this as your data

if you send this to the bridge, it will create a NativeArray and send it to native layer (But your layer's method expects String).

如果您将其发送到桥接器,它将创建一个 NativeArray 并将其发送到本机层(但您的层的方法需要字符串)。

Now you have to convert the object to String and send it to the native layer

现在您必须将对象转换为字符串并将其发送到本机层

const strData = JSON.stringify(data);

Now send the strData to the native layer

现在将 strData 发送到本机层

(Comment below if you need more details or you can tell your error more clearly to get some clear answer)

(如果您需要更多详细信息,请在下面评论,或者您可以更清楚地说出您的错误以获得一些明确的答案)