简体   繁体   English

启动画面后反应本机 expo 空白白屏

[英]react native expo blank white screen after splash screen

启动画面后,我得到这个白色的空白屏幕,它没有路由到登录组件或 DashBoard 组件 > app.json > app.json

{
  "expo": {
    "name": "Csearch",
    "slug": "Csearch",
    "version": "1.0.0",
    "orientation": "portrait",
    "sdkVersion": "38.0.0",
    "icon": "./assets/icon.png",
    "splash": {
      "image": "./assets/splash.png",
      "resizeMode": "contain",
      "backgroundColor": "#2e64ae"
    },
    "updates": {
      "fallbackToCacheTimeout": 0
    },
    "assetBundlePatterns": ["**/*"],
    "android": {
      "package": "com.geekonomy.Csearch",
      "versionCode": 1
    },
    "ios": {
      "supportsTablet": true
    },
    "web": {
      "favicon": "./assets/favicon.png"
    }
  }
}

package.json package.json

{
  "main": "node_modules/expo/AppEntry.js",
  "scripts": {
    "start": "expo start",
    "android": "expo start --android",
    "ios": "expo start --ios",
    "web": "expo start --web",
    "eject": "expo eject"
  },
  "dependencies": {
    "@react-native-community/masked-view": "0.1.10",
    "@react-navigation/drawer": "^5.8.6",
    "@react-navigation/native": "^5.7.1",
    "@react-navigation/stack": "^5.7.1",
    "expo": "~38.0.8",
    "expo-status-bar": "^1.0.2",
    "firebase": "^7.17.1",
    "native-base": "^2.13.13",
    "react": "~16.11.0",
    "react-dom": "~16.11.0",
    "react-native": "https://github.com/expo/react-native/archive/sdk-38.0.2.tar.gz",
    "react-native-easy-grid": "^0.2.2",
    "react-native-elements": "^2.0.4",
    "react-native-gesture-handler": "~1.6.0",
    "react-native-maps": "^0.27.1",
    "react-native-reanimated": "~1.9.0",
    "react-native-safe-area-context": "~3.0.7",
    "react-native-screens": "~2.9.0",
    "react-native-web": "~0.11.7",
    "react-router-native": "^5.2.0"
  },
  "devDependencies": {
    "@babel/core": "^7.8.6",
    "babel-preset-expo": "~8.1.0"
  },
  "private": true
}

App.js应用程序.js

export default class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      user: {},
      isReady: false,
    };
  }
  authListener() {
    fire.auth().onAuthStateChanged((user) => {
      if (user) {
        this.setState({ user });
      } else {
        this.setState({ user: null });
      }
    });
  }
  async componentDidMount() {
    await Font.loadAsync({
      Roboto: require("native-base/Fonts/Roboto.ttf"),
      Roboto_medium: require("native-base/Fonts/Roboto_medium.ttf"),
      ...Ionicons.font,
    });
    this.setState({ isReady: true }, () => {
      this.authListener();
    });
  }

  render() {
    if (!this.state.isReady) {
      return <AppLoading />;
    }
    return (
      <NativeRouter>
        <BackButton />
        <Switch>
          <Route exact path="/">
            {this.state.user ? <DashBoard /> : <Login />}
          </Route>
          <Route path="/criminalDetails" component={ShowSelectedCriminals}>
            {/* {this.state.user ? <ShowSelectedCriminals /> : <Login />} */}
          </Route>
          <Route path="/gangDetails" component={SelectedGangDetails}>
            {/* {this.state.user ? <SelectedGangDetails /> : <Login />} */}
          </Route>
          <Route path="/gangCriminalDetails" component={GangCriminalDetails}>
            {/* {this.state.user ? <GangCriminalDetails /> : <Login />} */}
          </Route>
          <Route path="/ShowGangs">
            {this.state.user ? <ShowGangs /> : <Login />}
          </Route>
          <Route path="/Showdata">
            {this.state.user ? <Showdata /> : <Login />}
          </Route>
        </Switch>
      </NativeRouter>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: "#fff",
    alignItems: "center",
    justifyContent: "center",
  },
});

Login.js登录.js

class Login extends Component {
  constructor(props) {
    super(props);
    this.login = this.login.bind(this);
    this.state = {
      email: "",
      password: "",
    };
  }
  login() {
    fire
      .auth()
      .signInWithEmailAndPassword(this.state.email, this.state.password)
      .then((u) => {
        console.log(u);
      })
      .catch((error) => {
        console.log(error);
      });
  }
  render() {
    return (
      <View style={styles.container}>
        {/* <Image
          style={{
            height: 25,
            width: 25,
          }}
          source={require("../assets/login.png")}
        /> */}
        <Item floatingLabel>
          <Label>Email</Label>
          <Input onChangeText={(email) => this.setState({ email })} />
        </Item>
        <Item floatingLabel last>
          <Label>Password</Label>
          <Input
            secureTextEntry={true}
            onChangeText={(password) => this.setState({ password })}
            placeholder="Password"
          />
        </Item>

        <Item
          style={{
            marginTop: 10,
            marginBottom: 10,
            justifyContent: "center",
            alignItems: "center",
            borderBottomColor: "white",
          }}
        >
          <Button info onPress={this.login}>
            <Text> Login </Text>
          </Button>
        </Item>
      </View>
    );
  }
}
const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: "#fff",
    alignItems: "center",
    justifyContent: "center",
    backgroundColor: "#2e64ae",
  },
});
export default Login;

DashBoard.js仪表板.js

const Drawer = createDrawerNavigator();
const DashBoardStack = createStackNavigator();
const ShowGangsStack = createStackNavigator();
const ShowCriminalsStack = createStackNavigator();
const DashBoardStackScreen = ({ navigation }) => (
  <DashBoardStack.Navigator
    screenOptions={{
      headerStyle: {
        backgroundColor: "#2e64ae",
      },
      headerTintColor: "#fff",
      headerTitleStyle: {
        fontWeight: "bold",
      },
    }}
  >
    <DashBoardStack.Screen
      name="DashBoard"
      component={InitialDb}
      options={{
        title: "CSearch",
        headerLeft: () => (
          <TouchableOpacity onPress={() => navigation.openDrawer()}>
            <Entypo name="menu" size={24} color="black" />
          </TouchableOpacity>
        ),
        headerRight: () => (
          <TouchableOpacity onPress={() => fire.auth().signOut()}>
            <AntDesign name="logout" size={24} color="black" />
          </TouchableOpacity>
        ),
      }}
    />
  </DashBoardStack.Navigator>
);
const ShowGangsStackStackScreen = ({ navigation }) => (
  <ShowGangsStack.Navigator
    screenOptions={{
      headerStyle: {
        backgroundColor: "#2e64ae",
      },
      headerTintColor: "#fff",
      headerTitleStyle: {
        fontWeight: "bold",
      },
    }}
  >
    <ShowGangsStack.Screen
      name="ShowGangs"
      component={ShowGangs}
      options={{
        title: "CSearch",
        headerLeft: () => (
          <TouchableOpacity onPress={() => navigation.openDrawer()}>
            <Entypo name="menu" size={24} color="black" />
          </TouchableOpacity>
        ),
        headerRight: () => (
          <TouchableOpacity onPress={() => fire.auth().signOut()}>
            <AntDesign name="logout" size={24} color="black" />
          </TouchableOpacity>
        ),
      }}
    />
  </ShowGangsStack.Navigator>
);
const ShowCriminalsStackScreen = ({ navigation }) => (
  <ShowCriminalsStack.Navigator
    screenOptions={{
      headerStyle: {
        backgroundColor: "#2e64ae",
      },
      headerTintColor: "#fff",
      headerTitleStyle: {
        fontWeight: "bold",
      },
    }}
  >
    <ShowCriminalsStack.Screen
      name="ShowCriminals"
      component={ShowData}
      options={{
        title: "CSearch",
        headerLeft: () => (
          <TouchableOpacity onPress={() => navigation.openDrawer()}>
            <Entypo name="menu" size={24} color="black" />
          </TouchableOpacity>
        ),
        headerRight: () => (
          <TouchableOpacity onPress={() => fire.auth().signOut()}>
            <AntDesign name="logout" size={24} color="black" />
          </TouchableOpacity>
        ),
      }}
    />
  </ShowCriminalsStack.Navigator>
);
export default class DashBoard extends React.Component {
  render() {
    return (
      <>
        {/* <Image
          style={{ width: 400, height: 400, borderRadius: 400 / 2 }}
          source={require("../assets/login.png")}
        /> */}
        <NavigationContainer>
          <BackButton />
          <Drawer.Navigator initialRouteName="DashBoard">
            <Drawer.Screen name="DashBoard" component={DashBoardStackScreen} />
            <Drawer.Screen
              name="ShowGangs"
              component={ShowGangsStackStackScreen}
            />
            <Drawer.Screen
              name="ShowCriminals"
              component={ShowCriminalsStackScreen}
            />
          </Drawer.Navigator>
        </NavigationContainer>
      </>
    );
  }
}

Here, in the above code, while running it in metro bundler, using expo run android command, the app is running properly in expo without any issue or error.在这里,在上面的代码中,在 metro bundler 中运行它时,使用 expo run android 命令,应用程序在 expo 中正常运行,没有任何问题或错误。 But When I build the apk using expo build:android, and try to install it in my physical device, then it's showing the splash screen and after that it is showing the white blank screen.但是当我使用 expo build:android 构建 apk 并尝试将其安装到我的物理设备中时,它会显示启动画面,然后显示白色空白屏幕。 Not routing to Login component or DashBoard component either也不路由到登录组件或仪表板组件

I think problem is here:我认为问题出在这里:

async componentDidMount() {
    await Font.loadAsync({
      Roboto: require("native-base/Fonts/Roboto.ttf"),
      Roboto_medium: require("native-base/Fonts/Roboto_medium.ttf"),
      ...Ionicons.font,
    });
    this.setState({ isReady: true }, () => {
      this.authListener();
    });
  }

looks like you get an error during Fonts loading and never set isReady to true看起来您在 Fonts 加载期间遇到错误并且从未将isReady设置为true

Try to rewrite it like this:尝试像这样重写它:

  _loadFontsAsync() {
    Font.loadAsync(customFonts).finally(() => {
      this.setState({ isReady: true }, () => {
        this.authListener();
      });
    });
  }

  componentDidMount() {
    this._loadFontsAsync();
  }

There are numerous solutions proposed but to actually understand your problem, you need to take a look at how Expo recommends debugging production issues .提出了许多解决方案,但要真正了解您的问题,您需要查看Expo 如何建议调试生产问题

Specifically, what I did was to run expo start --no-dev --minify and see if you can reproduce the issue.具体来说,我所做的是运行expo start --no-dev --minify并查看是否可以重现该问题。 (Note: this doesn't refresh when you change your code so you'd need to quit the Expo Go app and re-launch it to see the changes.) (注意:更改代码时不会刷新,因此您需要退出 Expo Go 应用程序并重新启动它以查看更改。)

For my issue, I was able to reproduce locally using the above method, and I was missing keyExtractor={(_, id) => id.toString()} in FlatList .对于我的问题,我能够使用上述方法在本地重现,并且我在 FlatList 中缺少keyExtractor={(_, id) => id.toString()} FlatList

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM