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.

2 回复

使用的是关系型数据库吗? 我这边使用测试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通常指代AbilityContextContext对象,用于提供应用运行时的上下文信息。在EntryAbility中,context可以通过this.contextthis直接获取。确保在初始化数据库时,传入的context参数是正确的AbilityContextContext对象,且不为null。如果context对象不正确或为空,系统会抛出Parameter error. The context must be object的异常。检查代码中数据库初始化部分的context参数传递,确保其正确性。

回到顶部