即使未使用null,createStackNavigator也会导致null不是对象(正在逃避'_RNGestureHandlerModule.default.direction)

时间:2019-11-06 08:03:52

标签: react-native react-native-navigation

我正在尝试使自己熟悉最新版本的反应导航。我已经将默认的本机项目版本设置为0.60.0。我注意到当我添加时:

  

从'react-navigation-stack'导入{createStackNavigator};

它立即使应用程序崩溃,并给我错误消息:

  

null不是对象(正在评估   '_RNGestureHandlerModule.defaultDirection)

即使我仅导入createStackNavigator,也会收到此错误消息。如果对导入进行注释,则该应用程序将正常运行。

我的package.json看起来像这样

{
  "name": "awesome",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "react-native start",
    "test": "jest",
    "lint": "eslint ."
  },
  "dependencies": {
    "react": "16.8.6",
    "react-native": "0.60.0",
    "react-native-gesture-handler": "^1.5.0",
    "react-native-reanimated": "^1.4.0",
    "react-native-screens": "1.0.0-alpha.23",
    "react-navigation": "^4.0.10",
    "react-navigation-stack": "1.5.1",
    "@react-navigation/core": "^3.5.0",
    "@react-navigation/native": "^3.6.2"
  },
  "devDependencies": {
    "@babel/core": "^7.7.0",
    "@babel/runtime": "^7.7.1",
    "@react-native-community/eslint-config": "^0.0.5",
    "babel-jest": "^24.9.0",
    "eslint": "^6.6.0",
    "jest": "^24.9.0",
    "metro-react-native-babel-preset": "^0.57.0",
    "react-test-renderer": "16.8.6"
  },
  "jest": {
    "preset": "react-native"
  }
}

我的App.js文件是标准的App文件,只有两个额外的导入行。

2 个答案:

答案 0 :(得分:0)

确保已安装

yarn add react-navigation
yarn add react-native-gesture-handler react-native-reanimated

然后更新MainActivity.java

package com.reactnavigation.example;

import com.facebook.react.ReactActivity;
+ import com.facebook.react.ReactActivityDelegate;
+ import com.facebook.react.ReactRootView;
+ import com.swmansion.gesturehandler.react.RNGestureHandlerEnabledRootView;

public class MainActivity extends ReactActivity {

  @Override
  protected String getMainComponentName() {
    return "Example";
  }

+  @Override
+  protected ReactActivityDelegate createReactActivityDelegate() {
+    return new ReactActivityDelegate(this, getMainComponentName()) {
+      @Override
+      protected ReactRootView createRootView() {
+       return new RNGestureHandlerEnabledRootView(MainActivity.this);
+      }
+    };
+  }
}

然后做:

cd android
gradlew clean
cd ..

然后照常做:

  

react-native run-android

答案 1 :(得分:0)

您可以检查安装列表

    yarn add react-native-reanimated react-native-gesture-handler react-native-screens@^1.0.0-alpha.23.

要在iOS上完成链接,请确保已安装Cocoapods。然后运行:

    cd ios
    pod install
    cd ..

要为react-native-screens完成Android的安装,请将以下两行添加到android/app/build.gradle:的“ dependencies”部分中

    implementation 'androidx.appcompat:appcompat:1.1.0-rc01'
    implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0-alpha02'

要为react-native-gesture-handler完成Android的安装,请对MainActivity.java:进行以下修改

    package com.reactnavigation.example;

    import com.facebook.react.ReactActivity;
    + import com.facebook.react.ReactActivityDelegate;
    + import com.facebook.react.ReactRootView;
    + import com.swmansion.gesturehandler.react.RNGestureHandlerEnabledRootView;

    public class MainActivity extends ReactActivity {

      @Override
      protected String getMainComponentName() {
        return "Example";
      }

    +  @Override
    +  protected ReactActivityDelegate createReactActivityDelegate() {
    +    return new ReactActivityDelegate(this, getMainComponentName()) {
    +      @Override
    +      protected ReactRootView createRootView() {
    +       return new RNGestureHandlerEnabledRootView(MainActivity.this);
    +      }
    +    };
    +  }
    }