gpt4 book ai didi

java - 向 android 日历插入一个新条目

转载 作者:行者123 更新时间:2023-11-30 04:31:48 24 4
gpt4 key购买 nike

我正在尝试将新事件添加到 android 日历中。这是我正在使用的代码

Intent intent = new Intent(Intent.ACTION_EDIT);
intent.setType("vnd.android.cursor.item/event");
intent.putExtra("title", event.getSummary());
intent.putExtra("description", event.getDescription());
intent.putExtra("eventLocation", event.getLocation());
intent.putExtra("dtstart", event.getStartDate());
if(event.getEndDate() == null)
{
intent.putExtra("allDay", true);
}
else
{
intent.putExtra("dtend", event.getEndDate());
}

startActivity(intent);

我在一个单独的类中实现这个方法,它不是一个 Activity 类。所以我在这里扩展 Activity 类。

当我执行最后一行 startActivity(intent);我收到 java 空指针异常。

不知道如何进行。

如何在 android 日历中添加条目?谢谢

最佳答案

您是否正在使用new 实例化您的 Activity 类?你不能这样做。 Activity 类必须由操作系统实例化才能发挥作用。

解决方法:

您必须引用 Context 才能调用 context.startActivity(..)。在构造函数中将上下文实例传递给您的类。提示:所有 Activity 都是上下文,因此只需在 Activity 中执行 new MyClass(this)

此外,您的类不需要扩展 Activity。

关于java - 向 android 日历插入一个新条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7584090/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com