5

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);
Cœur
  • 37,241
  • 25
  • 195
  • 267
najhi ullah
  • 213
  • 3
  • 7

4 Answers4

7

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);
    }
}
najhi ullah
  • 213
  • 3
  • 7
  • have a look at this issue: http://stackoverflow.com/questions/28871921/add-weekly-event-to-calendar – Sun Jul 01 '15 at 11:40
  • Throwing `java.lang.IllegalArgumentException: Event values must include an eventTimezone`. Worked when added `event.put(CalendarContract.Events.EVENT_TIMEZONE, CalendarContract.Calendars.CALENDAR_TIME_ZONE);` – Mudassir Nov 29 '17 at 05:37
2

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!

oikonomopo
  • 4,025
  • 7
  • 44
  • 73
0

Add Recurring Event in Android Calendar programatically:

Calendar calStart = Calendar.getInstance();
Calendar calEnd = Calendar.getInstance();
calEnd.add(Calendar.HOUR_OF_DAY, 2);

ContentResolver cr = mCtx.getContentResolver();
ContentValues values = new ContentValues();
TimeZone timeZone = TimeZone.getDefault();
values.put(CalendarContract.Events.DTSTART, calStart.getTimeInMillis());
values.put(CalendarContract.Events.DTEND, calEnd.getTimeInMillis());
values.put(CalendarContract.Events.EVENT_TIMEZONE, timeZone.getID());
values.put(CalendarContract.Events.TITLE, "Event Title");
values.put(CalendarContract.Events.DESCRIPTION, "Event Description");
values.put(CalendarContract.Events.CALENDAR_ID,Long.parseLong("Your_Calendar_Id"));

    if (Your_Event_Is_Daily) {
        values.put("rrule", "FREQ=DAILY");
    } else if (Your_Event_Is_Weekly) {
        values.put("rrule", "FREQ=WEEKLY");
    } else if (Your_Event_Is_Monthly) {
        values.put("rrule", "FREQ=MONTHLY");
    } else if (Your_Event_Is_Yearly) {
        values.put("rrule", "FREQ=YEARLY");
    }

Uri uri1 = cr.insert(CalendarContract.Events.CONTENT_URI, values);
String eventID = uri1.getLastPathSegment();
Log.i("Event Id", eventID);

Here eventID is Event Reference Id. If you need to update or delete event than eventID will be use.

Done

Hiren Patel
  • 52,124
  • 21
  • 173
  • 151
0

I would guess you should set DTEND and DURATION to some valid value, since "DTEND and DURATION cannot both be null for an event."

K-ballo
  • 80,396
  • 20
  • 159
  • 169