我在 Android 上的 react-native 应用程序中出现以下异常
E/unknown:ReactNative: Exception in native call
com.facebook.react.uimanager.IllegalViewOperationException: StackOverflowException
at com.facebook.react.ReactRootView.handleException(ReactRootView.java:563)
at com.facebook.react.views.view.ReactViewGroup.dispatchDraw(ReactViewGroup.java:669)
at android.view.View.draw(View.java:14468)
at android.view.View.getDisplayList(View.java:13362)
at android.view.View.getDisplayList(View.java:13404)
at android.view.View.draw(View.java:14182)
at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
at com.facebook.react.views.view.ReactViewGroup.dispatchDraw(ReactViewGroup.java:663)
at android.view.View.draw(View.java:14468)
at android.view.View.getDisplayList(View.java:13362)
at android.view.View.getDisplayList(View.java:13404)
at android.view.View.draw(View.java:14182)
at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
at com.facebook.react.views.view.ReactViewGroup.dispatchDraw(ReactViewGroup.java:663)
at android.view.View.getDisplayList(View.java:13357)
at android.view.View.getDisplayList(View.java:13404)
at android.view.View.draw(View.java:14182)
at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
at com.facebook.react.views.view.ReactViewGroup.dispatchDraw(ReactViewGroup.java:663)
at android.view.View.getDisplayList(View.java:13357)
at android.view.View.getDisplayList(View.java:13404)
at android.view.View.draw(View.java:14182)
at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
at com.facebook.react.views.view.ReactViewGroup.dispatchDraw(ReactViewGroup.java:663)
at android.view.View.getDisplayList(View.java:13357)
at android.view.View.getDisplayList(View.java:13404)
at android.view.View.draw(View.java:14182)
at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
at android.view.View.draw(View.java:14468)
at android.widget.FrameLayout.draw(FrameLayout.java:472)
at android.widget.ScrollView.draw(ScrollView.java:1603)
at com.facebook.react.views.scroll.ReactScrollView.draw(ReactScrollView.java:354)
at android.view.View.getDisplayList(View.java:13362)
at android.view.View.getDisplayList(View.java:13404)
at android.view.View.draw(View.java:14182)
at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
at com.facebook.react.views.view.ReactViewGroup.dispatchDraw(ReactViewGroup.java:663)
at android.view.View.draw(View.java:14468)
at android.view.View.getDisplayList(View.java:13362)
at android.view.View.getDisplayList(View.java:13404)
at android.view.View.draw(View.java:14182)
at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
at com.facebook.react.views.view.ReactViewGroup.dispatchDraw(ReactViewGroup.java:663)
at android.view.View.draw(View.java:14468)
at android.view.View.getDisplayList(View.java:13362)
at android.view.View.getDisplayList(View.java:13404)
at android.view.View.draw(View.java:14182)
at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
at android.view.View.getDisplayList(View.java:13357)
at android.view.View.getDisplayList(View.java:13404)
at android.view.View.draw(View.java:14182)
at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
at android.view.View.draw(View.java:14468)
at android.widget.FrameLayout.draw(FrameLayout.java:472)
at android.widget.HorizontalScrollView.draw(HorizontalScrollView.java:1609)
at com.facebook.react.views.scroll.ReactHorizontalScrollView.draw(ReactHorizontalScrollView.java:275)
at android.view.View.getDisplayList(View.java:13362)
at android.view.View.getDisplayList(View.java:13404)
at android.v
我的 React-Native 应用程序中有以下版本:
主要问题是,这个异常没有特定的情况,它只是发生在应用程序的任何地方。例如,当您在应用程序中待了一段时间,然后尝试转到某个屏幕时,它会因此异常而崩溃。
我搜索了很多关于
IllegalViewOperationException
.https://github.com/facebook/react-native/issues/13984 -> 这里的问题建议删除
LayoutAnimation
但是我已经从我的项目中完全删除了它,我仍然面临这个问题。com.facebook.react.uimanager.IllegalViewOperationException: Trying to add unknown view tag -> 这里的异常似乎是由另一个原因引起的,在我的情况下,我没有未知的 View 标签问题
我搜索了很多关于
StackOverflowException
在 react-native 中,但即使在它的 GitHub 问题中,结果也为零。我还检查了
Exception in native call
错误。例如,在此链接中,E/unknown:React: Exception in native call ,问题是由 bundle 引起的。但是我的情况与 bundle 无关。我可以在模拟器中成功运行应用程序并在屏幕之间导航。该异常也发生在真实设备上的 Release模式下。我找不到答案,包括
Exception in native call
, IllegalViewOperationException
, 和 StackOverflowException
关键词。该异常发生在只有 4.x 版本的 Android 设备上。它不会出现在 5 及更高版本的 Android 上。我猜这可能与设备内存不足有关,或者设备在连续页面导航中可能无法处理太多渲染。关于如何防止此异常的任何想法?
最佳答案
可能你有一个递归操作强制重新渲染。
https://github.com/facebook/react-native/blob/main/ReactAndroid/src/main/java/com/facebook/react/ReactRootView.java
@Override
protected void dispatchDraw(Canvas canvas) {
try {
super.dispatchDraw(canvas);
} catch (StackOverflowError e) {
// Adding special exception management for StackOverflowError for logging purposes.
// This will be removed in the future.
handleException(e);
}
}
https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/view/ViewGroup.javadispatchDraw
方法是 android SDK 的一部分。是 View 系统的一部分,允许在 viewGroup(根组件)内绘制其他 View 。由于进程的本质(因为 ViewGroup 也是一个 View,您可以在根内部嵌套无限的 View 树),StackoverflowException(VM 内存空间不足以进行另一个调用)是可能的。 React-Native 依赖于自定义 View 组件 ReactRootView,如果渲染了无限/过于昂贵的 View 树,它会抛出异常。 RN 应用程序的结构是一个 android Activity ,带有一个 ReactRootView(framelayout 子类),其中包含您在 JS 中添加的所有内容。每一个画面。因此,如果您的一个屏幕渲染次数过多而您渲染另一个屏幕,您就会到达 ReactRootView 溢出内存堆栈并出现异常的地步。您会在旧手机上看到这一点,要么是因为设备规范低于渲染过程对其施加的压力,要么是因为 RN 的 View 生成代码上的特定错误在 4.4.2 中暴露。
尝试使用Why-did-you-render 之类的渲染评估工具来检测不需要的渲染周期。否则,您需要减少渲染量。
关于android - react native StackOverflowException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53704643/