Inserting event to calendar - ContentResolver.Insert crash on Xamarin Android

dai15002dai15002 Member ✭✭
edited July 3 in Xamarin.Android

Hello. Using this tutorial (https://docs.microsoft.com/en-us/xamarin/android/user-interface/controls/calendar) I am trying to add an event to the Android calendar. However, the ContentResolver.Insert function crashes due to null context reference. I am trying to get the Application.Context as well but it still doesnt work. Any idea on how to get the proper context?

My solution:

[assembly: Xamarin.Forms.Dependency(typeof(AndroidCalendar))]
namespace AllInclusive.Droid
{
[Activity(Label = "AndroidCalendar")]
public class AndroidCalendar : Activity, IOSCalendarManager
{
public void AddCalendarEvent()
{

        ContentValues eventValues = new ContentValues();

        eventValues.Put(CalendarContract.Events.InterfaceConsts.CalendarId,
            0);
        eventValues.Put(CalendarContract.Events.InterfaceConsts.Title,
            "Test Event from M4A");
        eventValues.Put(CalendarContract.Events.InterfaceConsts.Description,
            "This is an event created from Xamarin.Android");
        eventValues.Put(CalendarContract.Events.InterfaceConsts.Dtstart,
            EventListActivity.GetDateTimeMS(2011, 12, 15, 10, 0));
        eventValues.Put(CalendarContract.Events.InterfaceConsts.Dtend,
            EventListActivity.GetDateTimeMS(2011, 12, 15, 11, 0));

        eventValues.Put(CalendarContract.Events.InterfaceConsts.EventTimezone,
            "UTC");
        eventValues.Put(CalendarContract.Events.InterfaceConsts.EventEndTimezone,
            "UTC");

        //crashes here with exception message Android.Database.Sqlite.SQLiteException
        var uri = Android.App.Application.Context.ContentResolver.Insert(CalendarContract.Events.ContentUri, eventValues);
    }

}

}

Sign In or Register to comment.