【问题标题】:React Native navigation 0.58.X on AndroidAndroid 上的 React Native 导航 0.58.X
【发布时间】:2019-02-05 20:36:03
【问题描述】:

我使用的是 Native v0.58.3,但我似乎无法让导航正常工作。

我似乎无法弄清楚为什么它不起作用,并且在调试和查看文档时找不到错误。我得到的错误是:

null 不是对象(评估 'rngesturehandlermodule.state')

运行此代码时:

/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 *
 * @format
 * @flow
 * @lint-ignore-every XPLATJSCOPYRIGHT1
 */

import React, {Component} from 'react';
import {widthPercentageToDP as wp, heightPercentageToDP as hp} from 'react-native-responsive-screen';
import {Platform, StyleSheet, Text, View, Image, ScrollView, Navigator, Button, TouchableOpacity, borderRadius} from 'react-native';
import {createStackNavigator, createAppNavigator} from 'react-navigation';
import TimeLine from './Screens/TimeLine';
import Home from './Screens/Home';

const RootStack = createStackNavigator(
{
  Home: { screen: Home },
  TimeLine: { screen: TimeLine },
},
{
    initialRouteName: 'Home',
}

);

type Props = {};
export default class App extends Component<Props> {
  render() {
    return <RootStack />;
  }
}

import React, {Component} from 'react';
import {widthPercentageToDP as wp, heightPercentageToDP as hp} from 'react-native-responsive-screen';
import {Platform, StyleSheet, Text, View, Image, ScrollView, Navigator, Button, TouchableOpacity, borderRadius} from 'react-native';
import {createStackNavigator, createAppNavigator, StackNavigator} from 'react-navigation';

const styles = StyleSheet.create({
  container: {flex: 1},
  Button: {
  backgroundColor: '#992632',
  marginTop: hp('6.665%'),
  marginBottom: hp('6,665%'),
  marginRight: wp('4%'),
  marginLeft: wp('4%'),
  height: hp('20%'),
  borderRadius: 150,
  }
});

export class Home extends Component {
  render() {
    return (
      <View style={{
            backgroundColor: '#0a6ca3',
            height: hp('100%'),
          }}>

      <TouchableOpacity
          onPress={() => this.props.navigation.navigate('TimeLine')}
 
          style={styles.Button}>
              <Text style = {{fontSize: 75, color: 'white', textAlign: 'center', marginTop: hp('4.5%')}}> Tidslinje </Text>
        </TouchableOpacity>

        <TouchableOpacity
          onPress={function(){
            console.log('Rasmus')
          }}
          style={styles.Button}>
            <Text style = {{fontSize: 75, color: 'white', textAlign: 'center', marginTop: hp('4.5%')}}> Diskussion </Text>
        </TouchableOpacity>

        <TouchableOpacity
          onPress={function(){
            console.log('Rasmus')
          }}
 
          style={styles.Button}> 
          <Text style = {{fontSize: 75, color: 'white', textAlign: 'center', marginTop: hp('4.5%')}}> Demokrati </Text>
        </TouchableOpacity>
      </View>
    )
  }
}

export default Home;

【问题讨论】:

  • 该错误通常是由链接问题引起的。您是否正确链接了代码?另外,您应该将RootStack 包装在createAppNavigator 中,例如const AppNav = createAppNavigator(RootStack);,并在渲染中使用AppNav 代替RootStack
  • 添加此代码后: const AppNav = createAppNavigator(RootStack);到我的项目它仍然给我同样的结果?
  • 我知道这不能解决您的问题。我正在纠正您的反应导航设置。该错误是由于未正确链接包引起的。您需要重新检查您的设置方式。
  • 此外,如果这发生在 Android、iOS 或两者上,您应该在问题中说明。因为这将有助于缩小问题所在
  • 谢谢您已更正,我找不到链接错误?

标签: android react-native navigation


【解决方案1】:

从上面的 cmets 可以清楚地看出,您尚未执行 Android 设置的最后阶段。

在项目的根目录中有一个名为android 的文件夹。您需要在该文件夹中查找名为 MainActivity.java 的文件,它可能位于 android/app/src/main/java/com/your_app_name/

在该文件中,您需要进行一些更改。所有旁边有加号的行都需要添加到文件中(显然你包括+

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);
+      }
+    };
+  }
}

你应该看看官方文档设置react-native-gesture-handlerhttps://kmagiera.github.io/react-native-gesture-handler/docs/getting-started.html

react-navigation也详细说明步骤https://reactnavigation.org/docs/en/getting-started.html

【讨论】:

  • :导入com.swmansion.gesturehandler.react.RNGestureHandlerEnabledRootView;运行时似乎返回错误?
  • 您可能需要在 Android Studio 中打开项目并让 gradle 完成它的工作。您还应该清理您的项目。
【解决方案2】:

这听起来像是一个链接问题。你看过这个react-native-gesture-handler 帖子了吗?

否则你可以运行

react-native link react-native-gesture-handler

并重新安装应用程序(即yarn run-ios)。

【讨论】:

  • 我使用的是 npm 而不是纱线,但是当我运行 'npm link react-native-gesture-handler' 时出现此错误:npm WARN react-native-gesture-handler@1.0.15 requires a react@> 15.0.0 的对等点,但没有安装。您必须自己安装对等依赖项。 npm WARN react-native-gesture-handler@1.0.15 需要 react-native@>= 0.50.0 的对等点,但没有安装。您必须自己安装对等依赖项。
  • 尝试运行 npm install,并验证是否安装了 react-native。该错误告诉我们既没有安装 react 也没有安装 react-native。
猜你喜欢
  • 1970-01-01
  • 2016-06-29
  • 2018-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多