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
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 call context.startActivity(..)
. Pass an instance of context to your class in constructor. Hint: all Activities are Contexts, so just do new MyClass(this)
from within an Activity.
Also, your class does not need to extend Activity.