向 Android 日历插入新条目
我正在尝试将新事件添加到 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);
我在一个单独的类中实现此方法,该类不是活动类。所以我在这里扩展活动课。
当我执行最后一行 startActivity(intent); 时我收到 java 空指针异常。
不知道如何继续。
如何在 Android 日历中添加条目? 谢谢
I am trying to add a new event into the android calendar. Here is the code I am using
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);
I am implementing this method in a separate class which is not an activity class. So I am extending the activity class here.
When i execute last line startActivity(intent); I am getting java nullpointer exception.
No idea how to proceed.
How could I add a entry in to android calendar?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否使用
new
实例化您的活动类?你不能那样做。活动类必须由操作系统实例化才能发挥作用。解决方案:
您必须引用
Context
才能调用context.startActivity(..)
。在构造函数中将上下文实例传递给您的类。提示:所有 Activity 都是上下文,因此只需在 Activity 中执行new MyClass(this)
即可。此外,您的类不需要扩展 Activity。
Are you instantiating your activity class with
new
? You can not do that. Activity classes must be instantiated by OS in order to be functional.Solution:
You have to have a reference to
Context
in order to callcontext.startActivity(..)
. Pass an instance of context to your class in constructor. Hint: all Activities are Contexts, so just donew MyClass(this)
from within an Activity.Also, your class does not need to extend Activity.