admin管理员组

文章数量:1291045

We have a react-native app (with expo, but not expo-go), where under certain conditions, the following exception appears and the app crashes.

I know, at some point we are passing a String where React-Native expects a Double. I really would like to know where. I tried with debugging the app-debug.apk. But when adding a breakpoint at the method in ReadableNativeArray, the App just hangs and the screen of my device stays black.

Is there any way to find out which value caused this exception? Or which code is responsible for this?

java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Double
    at com.facebook.react.bridge.ReadableNativeArray.getDouble(ReadableNativeArray.java:92)
    at com.facebook.react.bridge.JavaMethodWrapper$4.extractArgument(JavaMethodWrapper.java:64)
    at com.facebook.react.bridge.JavaMethodWrapper$4.extractArgument(JavaMethodWrapper.java:60)
    at com.facebook.react.bridge.JavaMethodWrapper.invoke(JavaMethodWrapper.java:356)
    at com.facebook.react.bridge.JavaModuleWrapper.invoke(JavaModuleWrapper.java:146)
    at com.facebook.jni.NativeRunnable.run(Native Method)
    at android.os.Handler.handleCallback(Handler.java:938)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:27)
    at android.os.Looper.loop(Looper.java:223)
    at com.facebook.react.bridge.queue.MessageQueueThreadImpl.lambda$startNewBackgroundThread$2(MessageQueueThreadImpl.java:217)
    at com.facebook.react.bridge.queue.MessageQueueThreadImpl$$ExternalSyntheticLambda1.run(D8$$SyntheticClass:0)
    at java.lang.Thread.run(Thread.java:923)

We have a react-native app (with expo, but not expo-go), where under certain conditions, the following exception appears and the app crashes.

I know, at some point we are passing a String where React-Native expects a Double. I really would like to know where. I tried with debugging the app-debug.apk. But when adding a breakpoint at the method in ReadableNativeArray, the App just hangs and the screen of my device stays black.

Is there any way to find out which value caused this exception? Or which code is responsible for this?

java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Double
    at com.facebook.react.bridge.ReadableNativeArray.getDouble(ReadableNativeArray.java:92)
    at com.facebook.react.bridge.JavaMethodWrapper$4.extractArgument(JavaMethodWrapper.java:64)
    at com.facebook.react.bridge.JavaMethodWrapper$4.extractArgument(JavaMethodWrapper.java:60)
    at com.facebook.react.bridge.JavaMethodWrapper.invoke(JavaMethodWrapper.java:356)
    at com.facebook.react.bridge.JavaModuleWrapper.invoke(JavaModuleWrapper.java:146)
    at com.facebook.jni.NativeRunnable.run(Native Method)
    at android.os.Handler.handleCallback(Handler.java:938)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:27)
    at android.os.Looper.loop(Looper.java:223)
    at com.facebook.react.bridge.queue.MessageQueueThreadImpl.lambda$startNewBackgroundThread$2(MessageQueueThreadImpl.java:217)
    at com.facebook.react.bridge.queue.MessageQueueThreadImpl$$ExternalSyntheticLambda1.run(D8$$SyntheticClass:0)
    at java.lang.Thread.run(Thread.java:923)
Share Improve this question asked Feb 13 at 15:08 Paul Wellner BouPaul Wellner Bou 5545 silver badges18 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

I found a solution:

  1. Patch ./node_modules/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/ReadableNativeArray.java adding more information to the exception:
  @Override
  public double getDouble(int index) {
      Object value = getLocalArray()[index];
      try {
          return ((Double) getLocalArray()[index]).doubleValue();
      } catch (ClassCastException e) {
          throw new ClassCastException(
              "Value at index " + index + " is not a number: " + value
          );
      }
  }
  1. Configure your android buildscript to compile React Native from source instead of using prebuilt aar files (see https://reactnative.dev/contributing/how-to-build-from-source), adding:
includeBuild('../node_modules/react-native') {
    dependencySubstitution {
        substitute(module("com.facebook.react:react-android")).using(project(":packages:react-native:ReactAndroid"))
        substitute(module("com.facebook.react:react-native")).using(project(":packages:react-native:ReactAndroid"))
        substitute(module("com.facebook.react:hermes-android")).using(project(":packages:react-native:ReactAndroid:hermes-engine"))
        substitute(module("com.facebook.react:hermes-engine")).using(project(":packages:react-native:ReactAndroid:hermes-engine"))
    }
}
  1. Build app and run

    本文标签: