React Native:应用程序在首次启动时崩溃,但重新启动后可以正常工作,并且 APK 显示“无法加载脚本”错误

问题描述 投票:0回答:1

我的 React Native 项目面临两个问题:

  1. 应用程序在首次启动时崩溃

    • 当我运行命令
      react-native run-android
      时,应用程序会在模拟器/设备上安装并运行,但在首次启动时崩溃。
    • 重新打开应用程序后,它运行良好,没有崩溃。但是,如果应用程序崩溃或重新启动(出于任何原因),它会再次崩溃。
  2. 构建 APK 时出错

    • 当我构建 APK 并安装它时,应用程序崩溃并在红色屏幕上显示以下错误:
    Unable to load script. Make sure you're either running Metro (run 'npx react-native start') or that your bundle 'index.android.bundle' is packaged correctly for release.
    
    • 应用程序根本打不开,我无法继续执行此错误。

这是我的

package.json
供参考:

{
  "name": "Connect",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "android": "react-native run-android",
    "ios": "react-native run-ios",
    "lint": "eslint .",
    "start": "react-native start",
    "test": "jest"
  },
  "dependencies": {
    "@react-native-async-storage/async-storage": "^1.24.0",
    "@react-navigation/bottom-tabs": "^6.6.1",
    "@react-navigation/drawer": "^6.7.2",
    "@react-navigation/native": "^6.1.18",
    "@react-navigation/native-stack": "^6.11.0",
    "axios": "^1.7.7",
    "react": "18.2.0",
    "react-native": "0.74.5",
    "react-native-calendars": "^1.1305.0",
    "react-native-dotenv": "^3.4.11",
    "react-native-gesture-handler": "^2.18.1",
    "react-native-linear-gradient": "^2.8.3",
    "react-native-reanimated-carousel": "^3.5.1",
    "react-native-responsive-dimensions": "^3.1.1",
    "react-native-safe-area-context": "^4.11.0",
    "react-native-screens": "^3.34.0",
    "react-native-svg": "^15.6.0",
    "react-native-vector-icons": "^10.2.0"
  },
  "devDependencies": {
    "@babel/core": "^7.20.0",
    "@babel/preset-env": "^7.20.0",
    "@babel/runtime": "^7.20.0",
    "@react-native/babel-preset": "0.74.87",
    "@react-native/eslint-config": "0.74.87",
    "@react-native/metro-config": "0.74.87",
    "@react-native/typescript-config": "0.74.87",
    "@types/react": "^18.2.6",
    "@types/react-test-renderer": "^18.0.0",
    "babel-jest": "^29.6.3",
    "eslint": "^8.19.0",
    "jest": "^29.6.3",
    "prettier": "2.8.8",
    "react-test-renderer": "18.2.0",
    "typescript": "5.0.4"
  },
  "engines": {
    "node": ">=18"
  }
}

我尝试了以下解决方案,但到目前为止都没有效果:

  • 在启动应用程序之前运行
    npx react-native start
  • 多次重建APK。

任何解决这些问题的帮助或建议将不胜感激。

你尝试了什么?

  • 已安装
    react-native-screens
    react-native-safe-area-context
    ,以及
    npm install
  • 删除了
    node_modules
    package-lock.json
    ,然后重新安装了依赖项。
  • 运行
    npm update
    并尝试升级和降级软件包版本。
  • 使用
    npx react-native start --reset-cache
    清除了 Metro 缓存。

你在期待什么?

  • 希望应用程序能够顺利运行,不会在首次启动时崩溃。
  • 预计 APK 能够构建并运行,而不会显示“无法加载脚本”错误或需要 Metro。
react-native build crash apk emulation
1个回答
0
投票

当您使用模拟器时,请查看网络连接情况。如果您未连接到网络,您会看到错误。

© www.soinside.com 2019 - 2024. All rights reserved.