错误:null 不是对象(正在评估“C.State”)<unknown> index.android.bundle

IT技术 android reactjs react-native react-navigation hybrid-mobile-app
2021-05-03 07:14:38

我是 React-Native 的新手。我正在将我现有的 Android 本机应用程序与 react 本机应用程序集成在一起。运行应用程序后的本机部分显示此错误:

null 不是对象(评估“C.State”)未知index.android.bundle

这是错误的 屏幕截图:错误消息的屏幕截图

我知道每次使用react-navigationreact-native-gesture-handler 时都会出现此错误消息

这是 package.json 文件的代码:

{
  "name": "Demo",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "node node_modules/react-native/local-cli/cli.js start",
    "test": "jest"
  },
  "dependencies": {
    "react": "16.6.3",
    "react-native": "^0.58.3",
    "react-native-gesture-handler": "^1.0.15",
    "react-navigation": "^3.1.4"
  },
  "devDependencies": {
    "babel-core": "^7.0.0-bridge.0",
    "babel-jest": "24.0.0",
    "jest": "24.0.0",
    "metro-react-native-babel-preset": "0.51.1",
    "react-test-renderer": "16.6.3"
  },
  "jest": {
    "preset": "react-native"
  }
}

这是呈现 react-native module的本机活动:

public class MyReactActivity extends Activity implements DefaultHardwareBackBtnHandler {
private ReactRootView mReactRootView;
private ReactInstanceManager mReactInstanceManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mReactRootView = new ReactRootView(this);
    mReactInstanceManager = ReactInstanceManager.builder()
            .setApplication(getApplication())
            .setBundleAssetName("index.android.bundle")
            .setJSMainModulePath("index.android")
            .addPackage(new MainReactPackage())
            .setUseDeveloperSupport(BuildConfig.DEBUG)
            .setInitialLifecycleState(LifecycleState.RESUMED)
            .build();
    Bundle initialProps = new Bundle();
    String str = MainActivity.getMovieID();
    initialProps.putString("movie_id", str);
    mReactRootView.startReactApplication(mReactInstanceManager, "AwesomeProject", initialProps);

    setContentView(mReactRootView);
}

观察上面代码中编写的 index.android.bundle 。

同样在运行应用程序之前,我运行此命令来解决无法从资产 index.android.bundle 加载脚本的问题

react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src /主要/资源

这里也观察到 index.android.bundle 正在做一些事情

找不到与此错误相关的任何内容。任何帮助将不胜感激。谢谢。

1个回答

我有一个类似的问题,我做了什么来修复它,它是阅读 React Navigation 文档,在这个页面中这是我的解决方案:

https://reactnavigation.org/docs/en/getting-started.html

之后,如果您的 settings.gradle 文件中有这样的错误:

FAILURE:构建失败,出现异常。

  • 在哪里:

设置文件 'C:\PATH\TO\YOUR\PROYECT\android\settings.gradle' 行:3...

在您的 settings.gradle 文件中,在您看到以下内容的行中将这些字符“\”更改为“/”:'..\node_modules\react-native-gesture-handler\android'

就是这样,希望我能帮上忙...