I am developing an application for adding events to calendar. I am using following code to insert recurring event but it force closes the application with an error:
"java.lang.IllegalArgumentException: DTEND and DURATION cannot both be null for an event."
code:
ContentValues event = new ContentValues();
event.put("calendar_id", 1);
event.put("title", "Event Title");
event.put("description", "Event Desc");
event.put("eventLocation", "Event Location");
event.put("dtstart", Long.parseLong("1315432844000"));
event.put("rrule", "FREQ=WEEKLY;WKST=SU;BYDAY=WE");
event.put("allDay", 1); // 0 for false, 1 for true
event.put("eventStatus", 1);
event.put("hasAlarm", 1); // 0 for false, 1 for true
Uri url = getContentResolver().insert(eventsUri, event);
Creating recurring events in Squarespace is easy. Simply go to the Events section of your website, and click on the “Add Event” button. From there, you'll be able to select “Recurring” from the event type dropdown menu. Once you've done that, you can fill out the rest of the event details as usual.
this is my corrected code..working fine :)
public class mainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Uri eventsUri;
if (android.os.Build.VERSION.SDK_INT <= 7) {
eventsUri = Uri.parse("content://calendar/events");
} else {
eventsUri = Uri.parse("content://com.android.calendar/events");
}
Calendar cal = Calendar.getInstance();
ContentValues event = new ContentValues();
event.put("calendar_id", 1);
event.put("title", "Event Title");
event.put("description", "Event Desc");
event.put("eventLocation", "Event Location");
event.put("dtstart",cal.getTimeInMillis());
event.put("rrule", "FREQ=WEEKLY;WKST=SU;BYDAY=WE");
event.put("allDay", 1); // 0 for false, 1 for true
event.put("eventStatus", 1);
event.put("hasAlarm", 1); // 0 for false, 1 for true
event.put("duration","P3600S");
Uri url = getContentResolver().insert(eventsUri, event);
}
}
From CalendarContract.Events ...
Insert
When inserting a new event the following fields must be included:
dtstart
dtend if the event is non-recurring
duration if the event is recurring
rrule or rdate if the event is recurring
eventTimezone
a calendar_id
So for a recurring event you must have dtstart,duration,rrule/rdate,eventTimezone,calendar_id.
So in your case
remove dtend!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With