日历功能异常,getCalendar失败

按照文档描述通过getCalendar获取日历,发现走了catch提示【GetCalendar failed!】

Cannot get SourceMap info, dump raw stack: 
=====================Backtrace======================== 
#01 pc 000000000001f0b3 /system/lib64/module/libcalendarmanager.z.so 
#02 pc 000000000001f31b /system/lib64/module/libcalendarmanager.z.so 
#03 pc 00000000000479eb /system/lib64/platformsdk/libace_napi.z.so 
#04 pc 0000000000012cbb /system/lib64/platformsdk/libuv.so 
#05 pc 00000000000172bb /system/lib64/platformsdk/libuv.so 
#06 pc 0000000000016c03 /system/lib64/platformsdk/libuv.so 
#07 pc 000000000001772b /system/lib64/platformsdk/libuv.so 
#08 pc 0000000000069047 /system/lib64/platformsdk/libruntime.z.so

完整代码如下:

import AbilityConstant from '@ohos.app.ability.AbilityConstant'; 
import hilog from '@ohos.hilog'; 
import UIAbility from '@ohos.app.ability.UIAbility'; 
import Want from '@ohos.app.ability.Want'; 
import window from '@ohos.window'; 
import calendarManager from '@ohos.calendarManager'; 
import { abilityAccessCtrl, common, PermissionRequestResult, Permissions } from '@kit.AbilityKit'; 
import { BusinessError } from '@kit.BasicServicesKit'; 
 
export let calendarMgr: calendarManager.CalendarManager | null = null; 
export let mContext: common.UIAbilityContext | null = null; 
export default class EntryAbility extends UIAbility { 
  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); 
  } 
 
  onDestroy(): void { 
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); 
  } 
 
  onWindowStageCreate(windowStage: window.WindowStage): void { 
    // Main window is created, set main page for this ability 
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); 
 
    windowStage.loadContent('pages/Index', (err, data) => { 
      if (err.code) { 
        hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); 
        return; 
      } 
      hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? ''); 
      mContext = this.context; 
      const permissions: Permissions[] = ['ohos.permission.READ_CALENDAR', 'ohos.permission.WRITE_CALENDAR']; 
      let atManager = abilityAccessCtrl.createAtManager(); 
      atManager.requestPermissionsFromUser(mContext, permissions).then((result: PermissionRequestResult) => { 
        console.log(`get Permission success, result: ${JSON.stringify(result)}`); 
        calendarMgr = calendarManager.getCalendarManager(mContext); 
        calendarMgr?.getCalendar().then((data: calendarManager.Calendar) => { 
          console.info(`Succeeded to get calendar, data -> ${JSON.stringify(data)}`); 
          const event: calendarManager.Event = { 
            title: "我是标题", 
            description: "我是描述", 
            reminderTime: [5], 
            type: calendarManager.EventType.NORMAL, 
            startTime: 10152459790900, 
            endTime: 10152603790900 
          }; 
          data.addEvent(event, (err: BusinessError, data: number): void => { 
            if (err) { 
              console.error(`Failed to addEvent, err -> ${JSON.stringify(err)}`); 
            } else { 
              console.info(`Succeeded to add event, id -> ${data}`); 
            } 
          }); 
        }).catch((err: BusinessError) => { 
          console.error(`Failed to get calendar, err -> ${JSON.stringify(err)}`); 
        }); 
      }).catch((error: BusinessError) => { 
        console.error(`get Permission error, error: ${JSON.stringify(error)}`); 
      }) 
    }); 
  } 
 
  onWindowStageDestroy(): void { 
    // Main window is destroyed, release UI related resources 
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); 
  } 
 
  onForeground(): void { 
    // Ability has brought to foreground 
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); 
  } 
 
  onBackground(): void { 
    // Ability has back to background 
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); 
  } 
}
HarmonyOS
2024-09-06 12:03:13
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
Heiang

1、建议将上述部分代码按照下列参考文档分开写(获取权限与calendarManager.Event)参考:https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-calendar-kit/js-apis-calendarManager.md

2、检查一下获取权限是否成功。

先校验权限是否获取成功,获取失败一般都是权限没获取到,或者获取顺序不正确。

分享
微博
QQ
微信
回复
2024-09-06 16:31:45
相关问题
系统日历方法使用会抛异常
300浏览 • 0回复 待解决
编辑器异常问题,比如方法跳转失败
378浏览 • 1回复 待解决
HarmonyOS 日历相关组件
271浏览 • 1回复 待解决
PA开发,如何添加日历提醒?
3897浏览 • 1回复 待解决
HarmonyOS CJCalendar 日历组件无法编译
587浏览 • 1回复 待解决
HarmonyOS有日历组件吗?
268浏览 • 1回复 待解决
HarmonyOS 希望官方提供日历组件
124浏览 • 1回复 待解决
JS如何添加日历日程提醒?
7785浏览 • 1回复 待解决
有没有调用日历的接口?
6185浏览 • 1回复 待解决
如何让日历显示的是农历日期?
502浏览 • 1回复 待解决
如何跳转到系统的日历应用?
294浏览 • 1回复 待解决
是否提供日历组件,你了解吗?
1704浏览 • 1回复 待解决
HarmonyOS 日期/日历/时间选择器开发
222浏览 • 1回复 待解决
HarmonyOS 弹窗异常问题
221浏览 • 1回复 待解决
Redistemplate报nullpointer异常
4812浏览 • 1回复 待解决
HarmonyOS 异常堆栈获取
270浏览 • 1回复 待解决