Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I got this error in my RN application in Android:

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)

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.

like image 302
DNB5brims Avatar asked Mar 07 '23 01:03

DNB5brims


1 Answers

In ReactNative you are calling a method of the native bridge which requires String parameters. But what you are actually sending is 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).

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

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

like image 113
Sujith Niraikulathan Avatar answered Mar 16 '23 17:03

Sujith Niraikulathan