我正在尝试将新事件添加到 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/