Ionic添加cordova-android@11时Android 12启动页报错求助
问题分析
报错核心原因是cordova-android@11的prepare.js脚本在读取themes.xml时,找不到Theme.App.SplashScreen样式节点,导致splashScreenTheme变量为null,后续forEach循环中调用find方法触发Cannot read properties of null (reading 'find')错误。
解决方案1:手动添加缺失的启动页主题样式
- 检查项目路径
platforms/android/app/src/main/res/values/themes.xml,若文件不存在则创建。 - 在文件中添加
Theme.App.SplashScreen样式节点,示例代码:
<?xml version="1.0" encoding="utf-8"?> <resources> <style name="Theme.App.SplashScreen" parent="Theme.SplashScreen"> <!-- 可根据需求添加默认属性,示例: <item name="windowSplashScreenBackground">@color/background</item> <item name="windowSplashScreenAnimatedIcon">@drawable/splash_icon</item> <item name="postSplashScreenTheme">@style/AppTheme</item> --> </style> </resources>
- 保存文件后重新执行命令:
cordova platform add android@11
解决方案2:临时修改cordova-android脚本修复
- 打开报错文件
E:\DEV\IONIC\donateApp_cord\node_modules\cordova-android\lib\prepare.js - 在
updateProjectSplashScreen函数的forEach循环前添加空值判断:
function updateProjectSplashScreen (platformConfig, locations) { // res/values/themes.xml const themes = xmlHelpers.parseElementtreeSync(locations.themes); const splashScreenTheme = themes.find('style[@name="Theme.App.SplashScreen"]'); // 添加空值判断,避免后续报错 if (!splashScreenTheme) return; [ 'windowSplashScreenAnimatedIcon', 'windowSplashScreenAnimationDuration', 'windowSplashScreenBackground', 'windowSplashScreenBrandingImage', 'windowSplashScreenIconBackgroundColor', 'postSplashScreenTheme' ].forEach(themeKey => { // 后续原有逻辑代码 }); }
- 保存修改后重新执行平台添加命令。
解决方案3:升级cordova-android到兼容版本
cordova-android@11存在该已知bug,升级到更高版本(如@12及以上)可修复:
cordova platform remove android cordova platform add android@12
注意:升级前需确认项目其他插件兼容目标cordova-android版本。
内容的提问来源于stack exchange,提问作者Nelson Chadali
相关产品推荐
相关产品推荐

