ReactNative错误解决方法总结

ReactNative错误解决方法总结
 最后更新于 2024年10月01日 23:01:11

1、问题: bundling failed: Error: Unable to resolve module './../react-transform-hmr/lib/index.js' #21548

// npmPackages
react: 16.5.0 => 16.5.0
react-native: 0.57.2 => 0.57.2
// npmGlobalPackages
react-native-cli: 2.0.1

解决:

// 先执行清除缓存命令
react-native start --reset-cache
// 再执行运行命令
react-native run-android

2、问题: 执行 react-native run-android 命令,将 App 推送到 模拟器真机 时,出现下面的错误

Error type 3
Error: Activity class {com.yaruiapp/com.yaruiapp.MainActivity} does not exist.

解决:

// 第一步:adb命令清除设备上的apk包
D:> adb uninstall com.yaruiapp
// 第二步:在 Android Studio 中 `build/Clear project` 清除项目缓存

3、问题:WARNING: Configuration 'compile' is obsolete and has been replaced with 'implementation' and 'api'.原因: Android Studio升级3.1后提示的 解决:

compile => implementation
androidTestCompile => androidTestImplementation
testCompile => testImplementation

4、问题:The specified Android SDK Build Tools version (23.0.1) is ignored, as it is below the minimum supported version (28.0.3) for Android Gradle Plugin 3.3.1.

解决: 将所有的


5、问题: Android 关闭备份模式 allowBackup=false解决:AndroidManifest.xml 配置文件中显式配置 android:allowBackup=false

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" // 增加该条
/>

<application
  android:name=".ui.application.RiseApp"
  android:allowBackup="true"
  android:icon="@mipmap/ic_launcher"
  android:label="@string/app_name"
  android:supportsRtl="true"
  tools:replace="android:allowBackup" // 显示配置该条
  android:theme="@style/AppTheme">

6、问题:

Execution failed for task ':app:transformClassesWithMultidexlistForDebug'.
> com.android.build.api.transform.TransformException: Error while generating the main dex list:
  Error while merging dex archives:
  Learn how to resolve the issue at https://developer.android.com/studio/build/dependencies#duplicate_classes.
  Program type already present: com.remobile.toast.BuildConfig

解决:android/app/build.gradle 文件中注意是否重复引入了第三方类库


7、问题: Support for the experimental syntax 'decorators-legacy' isn't currently enabled解决:.babelrc 中新增

"plugins": [
    [
      "@babel/plugin-proposal-decorators",
      {
        "legacy": true
      }
    ]
  ]

8、问题:

java.util.concurrent.ExecutionException: com.android.builder.internal.aapt.v2.Aapt2InternalException: AAPT2 aapt2-3.3.1-5013011-windows Daemon
#2: Unexpected error during compile 'D:\work\xlz\React Native\qianfeng_user\android\app\src\main\res\drawable-xhdpi\launch_screen.png', attempting to stop daemon.

解决: 注意报错信息中有文件目录,查看该文件发现文件过大,转换后体积变小,执行通过


9、问题: 父组件传递新state给子组件时,子组件的 componentWillReceiveProps 生命周期不触发

解决:

Loading...

当父组件的数据是 Array 时,更新了内部一条数据或者删除、增加了一条数据,需要创建一个新对象赋值给 state。即:

this.state = {
  dataSource: [{ label: '周一', value: 0 }]
};

// 新数据和旧数据不指向同一内存区域
let _newData = this.state.dataSource.slice(0);
_newData[0].label = '周日'
this.setState({ dataSource: newData });

10、问题: state 或 props 更新后, 组件不渲染新数据

解决:

Loading...

FlatList 组件的数据更新了内部一条数据内容时,props=== 比较时没有发现出现变化,所以不更新。解决方案:

<FlatList
  data={this.state.dataSource}
  extraData={this.data}/>

// or

<FlatList
  data={this.props.dataSource}
  extraData={props.data}/>

11、问题: 安装运行 react-native-device-info 时出现 Error:Failed to resolve: com.android.support:support-core-ui:27.1.1解决:

// android/build.gradle
allprojects {
  repositories {
    google()
    mavenLocal()
    jcenter()
    maven {
      // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
      url "$rootDir/../node_modules/react-native/android"
    }

    maven {
      url 'https://maven.google.com/'
      name 'Google'
    }
    maven {
      url "https://jitpack.io"
    }
  }
}

12、问题: com.android.builder.internal.aapt.v2.Aapt2Exception: Android resource linkin解决:android/build.gradle 中新增下面的代码块

subprojects {
    afterEvaluate {project ->
        if (project.hasProperty("android")) {
            android {
                compileSdkVersion 28
                buildToolsVersion '28.0.3'
            }
        }
    }
}

13、问题: 静态资源打包构建,然后在Android项目的Java文件中使用 示例:

Loading...

// android/app/src/main/java/cdx/driverclient/MainActivity.java

import cdx.driverclient.R;

public void initPool() {
  poolMap = new HashMap<String, Integer>();
  // 实例化SoundPool,大小为3
  pool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
  // 装载音频进音频池,并且把ID记录在Map中
  poolMap.put("neworder", pool.load(this, R.raw.neworder, 1));
}

解决:

执行下面的命令,自动构建静态资源 java类/android/app/build目录中

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/main/res/

注意 若执行上述命令后,react-native run-android 运行项目任然出现资源类不存在(报错信息是 符号不存在),需要把 android\app\src\main\assets 目录下的 index.android.bundle 文件删除后再次执行上述命令


14、问题: 找不到头文件 'xx.h' file not found解决:

打开工程,选择 Target -> Build Settings 菜单,找到 Header Search Paths 设置项新增一个值 "${SRCROOT}",并且选择 Recursive