HarmonyOS 鸿蒙Next数据库在EntryAbility里初始化就报错提示Parameter error. The context must be object
HarmonyOS 鸿蒙Next数据库在EntryAbility里初始化就报错提示Parameter error. The context must be object
【设备信息】Mate60pro
【API版本】Api12
【DevEco Studio版本】5.0.5.300
【问题描述】
数据库在EntryAbility里初始化就报错,提示Parameter error. The context must be object.
使用的是关系型数据库吗? 我这边使用测试demo并未报错,建议补充可复现demo。 demo:
import UIAbility from '@ohos.app.ability.UIAbility';
import window from '@ohos.window';
import { BusinessError } from "@ohos.base";
let store: relationalStore.RdbStore | undefined = undefined;
class EntryAbility extends UIAbility {
onWindowStageCreate(windowStage: window.WindowStage) {
const STORE_CONFIG: relationalStore.StoreConfig = {
name: "RdbTest.db",
securityLevel: relationalStore.SecurityLevel.S1
};
relationalStore.getRdbStore(this.context, STORE_CONFIG).then(async (rdbStore: relationalStore.RdbStore) => {
store = rdbStore;
console.info('Get RdbStore successfully.')
}).catch((err: BusinessError) => {
console.error(`Get RdbStore failed, code is ${err.code},message is ${err.message}`);
})
}
}
参考资料:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-data-relationalstore-V5
也可以参考Codelabs中的关系型数据库:https://developer.huawei.com/consumer/cn/codelabsPortal/carddetails/tutorials_NEXT-Rdb
更多关于HarmonyOS 鸿蒙Next数据库在EntryAbility里初始化就报错提示Parameter error. The context must be object的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS中,EntryAbility
是应用的入口Ability,负责应用的生命周期管理。当在EntryAbility
中初始化数据库时,报错提示Parameter error. The context must be object
,通常是因为传入的context
参数不符合预期。在鸿蒙系统中,context
通常指代AbilityContext
或Context
对象,用于提供应用运行时的上下文信息。在EntryAbility
中,context
可以通过this.context
或this
直接获取。确保在初始化数据库时,传入的context
参数是正确的AbilityContext
或Context
对象,且不为null
。如果context
对象不正确或为空,系统会抛出Parameter error. The context must be object
的异常。检查代码中数据库初始化部分的context
参数传递,确保其正确性。