HarmonyOS鸿蒙Next中创建浮动子窗口,报错failed to init or restore uicontent with file
HarmonyOS鸿蒙Next中创建浮动子窗口,报错failed to init or restore uicontent with file 想添加一个浮动窗口,管理音频播放。官网下载的demo单独运行ok,放到项目feature功能模块下运行一直有问题。
1,项目用的Navigation导航,默认使用route_map.json配置运行报错failed to init or restore uicontent with file pages/MusicSubWindow, code:401,
2,两种方式同用的话,module.json5就是这样的
"pages": "$profile:main_pages",
"routerMap": "$profile:route_map"
运行报错
ERROR: AdaptorError: 00303038 Configuration Error。
instancePath: ‘module’,
keyword: ‘propertyNames’,
params: { propertyName: ‘pages’ },
message: ‘property name must be valid’,
应该采用什么方案,是哪里配置的不对
更多关于HarmonyOS鸿蒙Next中创建浮动子窗口,报错failed to init or restore uicontent with file的实战教程也可以访问 https://www.itying.com/category-93-b0.html
开发者您好,
请问参考的demo是悬浮窗效果-关键场景示例-影音娱乐类行业实践 - 华为HarmonyOS开发者嘛。
feature功能模块是指创建的feature功能模块还是feature文件夹呢,能否提供详细的代码和报错信息呢?
-
feature如果是hap包,本地测试也可以正常创建子窗口,代码如下:
main_pages.json
{ "src": [ "pages/Index", "subwindow/MusicSubWindow" ] }{ "routerMap": [ { "name": "PageThree", "pageSourceFile": "src/main/ets/pages/PageThree.ets", "buildFunction": "PageThreeBuilder", "data": { "description": "this is PageThree" } } ] }{ "module": { "name": "application", "type": "feature", "description": "$string:module_desc", "mainElement": "ApplicationAbility", "deviceTypes": [ "phone" ], "deliveryWithInstall": true, "installationFree": false, "pages": "$profile:main_pages", "routerMap": "$profile:router_map", "abilities": [ { "name": "ApplicationAbility", "srcEntry": "./ets/applicationability/ApplicationAbility.ets", "description": "$string:ApplicationAbility_desc", "icon": "$media:layered_image", "label": "$string:ApplicationAbility_label", "startWindowIcon": "$media:startIcon", "startWindowBackground": "$color:start_window_background", "exported": true } ] } }import { common, Want } from '@kit.AbilityKit'; // Index.ets @Entry @Component struct NavigationExample { pageInfos: NavPathStack = new NavPathStack(); build() { Navigation(this.pageInfos) { Column() { Button('pushPath PageThree', { stateEffect: true, type: ButtonType.Capsule }) .width('80%') .height(40) .margin(20) .onClick(() => { // 在源Ability中跳转 this.pageInfos.pushPath({ name: 'PageThree' }); // 将name指定的NavDestination页面信息入栈 }) } }.title('NavIndex') } }import { window } from "@kit.ArkUI"; import { hilog } from "@kit.PerformanceAnalysisKit"; import { Constants } from "../constants/Constants"; // PageThree.ets @Builder export function PageThreeBuilder(name: string, param: Object) { PageThree() } @Component export struct PageThree { pathStack: NavPathStack = new NavPathStack(); @State windowStage: window.WindowStage = AppStorage.get('windowStage1') as window.WindowStage; createSubWindow() { this.windowStage.createSubWindow(Constants.MUSIC_SUBWINDOW, (err, windowClass) => { if (err.code > 0) { hilog.error(0x0000, 'testTag', '%{public}s', `failed to create subWindow Cause: ${err.message}`); return; } try { // 设置子窗口加载页 windowClass.setUIContent('subwindow/MusicSubWindow', () => { windowClass.setWindowBackgroundColor(Constants.WINDOW_BACKGROUND_COLOR); }); // 设置子窗口左上角坐标 windowClass.moveWindowTo(Constants.INIT_SUBWINDOW_POSITION_X, Constants.INIT_SUBWINDOW_POSITION_Y); // 设置子窗口大小 windowClass.resize(this.getUIContext() .vp2px(Constants.INIT_SUBWINDOW_SIZE), this.getUIContext() .vp2px(Constants.INIT_SUBWINDOW_SIZE)); // 展示子窗口 windowClass.showWindow(); } catch (err) { hilog.error(0x0000, 'testTag', '%{public}s', `failed to create subWindow Cause:${err}`); } }); } build() { NavDestination() { Column() { Text('PageThree') Button('创建子窗口').onClick((event: ClickEvent) => { this.createSubWindow() }) }.width('100%').height('100%') }.title('PageThree') .onBackPressed(() => { this.pathStack.pop(); return true; }) .onReady((context: NavDestinationContext) => { this.pathStack = context.pathStack; console.info(`current page config info is ${JSON.stringify(context.getConfigInRouteMap())}`); }) } } -
如果创建的功能模块是Har包,entry模块中创建子窗口并设置页面可以通过命名路由实现,loadContentByName加载HAR包/HSP中页面。参考代码:
import { window } from "@kit.ArkUI"; import { hilog } from "@kit.PerformanceAnalysisKit"; import { Constants } from "../constants/Constants"; import 'library/src/main/ets/subwindow/MusicSubWindow2'; // PageTwo.ets @Builder export function PageTwoBuilder(name: string, param: Object) { PageTwo() } @Component export struct PageTwo { pathStack: NavPathStack = new NavPathStack(); @State windowStage: window.WindowStage = AppStorage.get('windowStage') as window.WindowStage; createSubWindow() { this.windowStage.createSubWindow(Constants.MUSIC_SUBWINDOW, (err, windowClass) => { if (err.code > 0) { hilog.error(0x0000, 'testTag', '%{public}s', `failed to create subWindow Cause: ${err.message}`); return; } try { // 设置子窗口加载页 windowClass.loadContentByName('MusicSubWindow2', () => { // windowClass.setUIContent('subwindow/MusicSubWindow', () => { windowClass.setWindowBackgroundColor(Constants.WINDOW_BACKGROUND_COLOR); }); // 设置子窗口左上角坐标 windowClass.moveWindowTo(Constants.INIT_SUBWINDOW_POSITION_X, Constants.INIT_SUBWINDOW_POSITION_Y); // 设置子窗口大小 windowClass.resize(this.getUIContext() .vp2px(Constants.INIT_SUBWINDOW_SIZE), this.getUIContext() .vp2px(Constants.INIT_SUBWINDOW_SIZE)); // 展示子窗口 windowClass.showWindow(); } catch (err) { hilog.error(0x0000, 'testTag', '%{public}s', `failed to create subWindow Cause:${err}`); } }); } build() { NavDestination() { Column() { Text('PageTwo') Button('创建子窗口').onClick((event: ClickEvent) => { this.createSubWindow() }) }.width('100%').height('100%') }.title('PageTwo') .onBackPressed(() => { this.pathStack.pop(); return true; }) .onReady((context: NavDestinationContext) => { this.pathStack = context.pathStack; console.info(`current page config info is ${JSON.stringify(context.getConfigInRouteMap())}`); }) } }// library/Index.ets export { MainPage } from './src/main/ets/components/MainPage'; export { MusicSubWindow2 } from './src/main/ets/subwindow/MusicSubWindow2'@Entry({ routeName: 'MusicSubWindow2' }) @Component export struct MusicSubWindow2 { // xxx }
更多关于HarmonyOS鸿蒙Next中创建浮动子窗口,报错failed to init or restore uicontent with file的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
"pages": "$profile:main_pages",
"routerMap": "$profile:route_map"
我好像记得只能二选一。删除**“pages”: “$profile:main_pages”,**这个试试看。
在HarmonyOS Next中,创建浮动子窗口时出现"failed to init or restore uicontent with file"错误,通常是由于UI内容文件路径配置不正确或文件不存在导致的。请检查以下方面:
- 确认UI内容文件路径是否正确配置
- 验证文件是否存在于指定位置
- 检查文件权限设置是否允许访问
- 确保UI内容文件格式符合规范要求
需要具体检查UI内容文件的加载配置和资源管理设置。
这个错误通常是由于 module.json5 中同时配置了 pages 和 routerMap 导致的冲突。在 HarmonyOS Next 中,pages 和 routerMap 是互斥的配置项,不能同时使用。
解决方案:
- 移除
pages配置:由于你使用了 Navigation 导航并配置了routerMap,应删除module.json5中的"pages": "$profile:main_pages"这一行。 - 确保
route_map.json配置正确:在src/main/resources/base/profile/目录下的route_map.json文件中,必须正确定义浮动子窗口的页面路由。例如,对于pages/MusicSubWindow,配置应类似:{ "routerMap": [ { "name": "MusicSubWindow", "pageSourceFile": "pages/MusicSubWindow" } ] } - 检查子窗口的 UIContent 加载:确保在创建子窗口时,传入的
windowName参数与route_map.json中定义的name一致。例如:let subWindow = await window.createWindow("MusicSubWindow", ...);
关键点:
- 使用 Navigation 导航时,只能配置
routerMap,不能配置pages。 failed to init or restore uicontent with file错误通常是因为系统在routerMap中找不到对应的页面定义。Configuration Error错误直接由pages和routerMap的配置冲突引起。
调整后,module.json5 中应仅保留:
"routerMap": "$profile:route_map"
并确保 route_map.json 包含子窗口页面的正确映射。


