升级react-native-gesture-handler至2.9.0后,RNGestureHandlerEnabledRootView不兼容报错
解决react-native-gesture-handler 2.9.0版本中RNGestureHandlerEnabledRootView不支持的问题
问题原因
react-native-gesture-handler从2.x版本开始彻底废弃了手动创建RNGestureHandlerEnabledRootView的方式,改为自动处理根视图的手势注入,旧版本的配置代码会触发找不到类或者启动崩溃的错误。
具体修复步骤
移除MainActivity.java中的自定义
createReactActivityDelegate方法
删掉你添加的这段代码,恢复MainActivity的默认结构:@Override protected ReactActivityDelegate createReactActivityDelegate() { return new ReactActivityDelegateWrapper(this, new ReactActivityDelegate(this, getMainComponentName()) { @Override protected ReactRootView createRootView() { return new RNGestureHandlerEnabledRootView(MainActivity.this); } }); }同时移除无用的
com.swmansion.gesturehandler.RNGestureHandlerPackage导入语句。确保手势库初始化正确
在你的App入口文件(App.js/App.tsx)的最顶部(所有其他导入之前)添加初始化代码:import 'react-native-gesture-handler';验证自动链接(React Native 0.60+)
新版本RN已支持自动链接,无需额外配置链接脚本。如果之前手动配置过链接,可以清理相关配置。
修改后的MainActivity.java示例
package com.your_app_package; import com.facebook.react.ReactActivity; public class MainActivity extends ReactActivity { @Override protected String getMainComponentName() { return "YourAppName"; // 替换成你的应用名称 } }
完成以上步骤后,重新编译运行应用即可解决崩溃和编译错误。
内容的提问来源于stack exchange,提问作者Preetika
相关产品推荐
相关产品推荐

