HarmonyOS鸿蒙Next中HdsActionBar组件的hoverTips属性支不支持跨包字符串资源?
HarmonyOS鸿蒙Next中HdsActionBar组件的hoverTips属性支不支持跨包字符串资源? 【问题描述】:HdsActionBar组件的hoverTips属性支不支持跨包字符串资源?我跨包使用会闪退。错误码:9001001
【问题现象】:HdsActionBar组件的hoverTips属性支不支持跨包字符串资源?我跨包使用会闪退。错误码:9001001
【版本信息】:Build Version: 6.0.2.640, built on January 19, 2026
【复现代码】:不涉及
【尝试解决方案】:根据错误码,使用了getStringByName()方法,通过这个方法,获取了跨包的值,在去赋值一样会闪退。
更多关于HarmonyOS鸿蒙Next中HdsActionBar组件的hoverTips属性支不支持跨包字符串资源?的实战教程也可以访问 https://www.itying.com/category-93-b0.html
开发者您好,参考官方FAQ:通过resourceManager.getStringResource接口获取HSP资源文件报“Resource id invalid”错误。
根据模块名创建上下文Context,然后使用getStringByNameSync方法获取指定资源名称对应的字符串。
已验证hoverTips可以跨包获取字符串资源:
import { hilog } from '@kit.PerformanceAnalysisKit';
import { ActionBarButton, ActionBarStyle, HdsActionBar } from '@kit.UIDesignKit';
import { application } from '@kit.AbilityKit';
@Entry
@ComponentV2
struct Index {
@Local isExpand: boolean = true;
@Local isPrimaryIconChanged: boolean = false;
@Local primaryHoverTips: ResourceStr = $r('app.string.local_stop_connection');
@Local libraryContext?:Context;
aboutToAppear(): void {
application.createModuleContext(this.getUIContext().getHostContext(), "library").then((moduleContext:Context) => {
this.libraryContext = moduleContext;
})
}
build() {
Column(){
Text($r('[library].string.stop_connection'))
Row() {
Column() {
HdsActionBar({
startButtons: [new ActionBarButton({
baseIcon: $r('sys.symbol.stopwatch_fill')
})],
endButtons: [new ActionBarButton({
baseIcon: $r('sys.symbol.mic_fill')
})],
primaryButton: new ActionBarButton({
baseIcon: $r('sys.symbol.plus'),
altIcon: $r('sys.symbol.play_fill'),
onClick: () => {
this.isExpand = !this.isExpand;
this.isPrimaryIconChanged = !this.isPrimaryIconChanged;
try {
if (this.isPrimaryIconChanged) {
this.primaryHoverTips = this.libraryContext?.resourceManager.getStringByNameSync('stop_connection')||'';
// this.primaryHoverTips = $r('[library].string.stop_connection');
} else {
this.primaryHoverTips = this.libraryContext?.resourceManager.getStringByNameSync('start_connection')||'';
// this.primaryHoverTips = $r('[library].string.start_connection');
}
console.log(this.primaryHoverTips+'====this.primaryHoverTips')
}catch (e) {
console.error(`getStringByNameSync failed, error code: ${e.code}, message: ${e.message}.`);
}
},
hoverTips: this.primaryHoverTips
}),
actionBarStyle: new ActionBarStyle({
isPrimaryIconChanged: this.isPrimaryIconChanged
}),
isExpand: this.isExpand!!
})
}
.width('100%')
}
}
}
}
更多关于HarmonyOS鸿蒙Next中HdsActionBar组件的hoverTips属性支不支持跨包字符串资源?的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
HdsActionBar组件的hoverTips属性支持跨包字符串资源引用。在鸿蒙Next中,可通过$r(‘app.type.name’)方式引用跨包资源,其中app需替换为实际资源包名。该属性用于设置组件悬停提示文本,支持字符串或资源引用。
根据您提供的信息,HdsActionBar组件的hoverTips属性在HarmonyOS Next当前版本(6.0.2.640)中,不支持直接引用跨包(即其他HAP/HSP模块)的字符串资源。这是导致应用闪退并出现错误码9001001的根本原因。
您遇到的错误码9001001通常与资源访问越界或资源ID解析失败有关。您尝试的getStringByName()方法虽然能获取到跨包字符串的内容,但将获取到的字符串值直接赋给hoverTips属性,系统在底层可能仍然会尝试将其作为资源ID进行解析或进行其他与资源上下文相关的校验,从而导致崩溃。
当前可行的解决方案是: 将需要跨包使用的字符串资源,复制或迁移到使用HdsActionBar组件的同一个HAP/HSP模块(包)的资源目录下。这是最直接、最稳定的解决方法,可以确保资源引用的正确性。
跨包资源访问在HarmonyOS中需要满足特定的条件(如通过@ohos.resourceManager接口显式查询),并且并非所有UI组件的属性都设计为支持这种动态获取的资源值。hoverTips这类属性通常期望一个编译时确定的本地资源引用。

