简体   繁体   中英

Xamarin.Forms Android - Can't add event to calendar “My calendar” programmatically

I have an app written in Xamarin.Forms which is able to create calendar events (although the calendar code is Android specific). I have confirmed that the code works for most calendars, but on my two physical phones, there exists a calendar called "My calendar" that I cannot add to.

I have confirmed that the value for key calendar_access_level corresponds to one of the following fields:
CalendarAccess.AccessContributor ,
CalendarAccess.AccessEditor ,
CalendarAccess.AccessOwner ,
CalendarAccess.AccessRoot (See CalendarAccessLevel , CalendarAccessLevel values )

Am I including an access level that I should not? Or is "My calendar" some kind of special calendar that I need to filter out?

I can add to it manually in the stock Calendar app, just not programmatically. I can add to any other calendars programmatically, so I don't believe it's an issue with that code.

Update : After getting the app to debug on an physical device that was having the issue, I was able to figure out the issue. Here is a partial stack trace (the rest is unrelated to the issue, it's just the gesture recognizer that called the command):

Exception: java.lang.NullPointerException
id == null

at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Users/builder/data/lanes/4009/f3074d2c/source/mono/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143
at Java.Interop.JniEnvironment+StaticMethods.CallStaticObjectMethod (Java.Interop.JniObjectReference type, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x00082] in /Users/builder/data/lanes/4009/9578cdcd/source/Java.Interop/src/Java.Interop/Java.Interop/JniEnvironment.g.cs:12649
at Java.Interop.JniPeerMembers+JniStaticMethods.InvokeObjectMethod (System.String encodedMember, Java.Interop.JniArgumentValue* parameters) [0x0001b] in /Users/builder/data/lanes/4009/9578cdcd/source/Java.Interop/src/Java.Interop/Java.Interop/JniPeerMembers.JniStaticMethods.cs:97
at Java.Util.TimeZone.GetTimeZone (System.String id) [0x0001e] in /Users/builder/data/lanes/4009/9578cdcd/source/monodroid/src/Mono.Android/platforms/android-23/src/generated/Java.Util.TimeZone.cs:402
at MyProject.Droid.CalendarEventsAndroid+<CreateCalendarEventsAsync>d__4.MoveNext () [0x000bf] in C:\Projects\MyProject\MyProject.App\MyProject\Droid\DependencyServices\CalendarEventsAndroid.cs:64 

After finding a physical device that was having the issue, I was able to reproduce it while debugging and solve it. The issue is that "My calendar" did not have a timezone assigned to it, I'm assuming because it's a default calendar and inherits the user's timezone setting. Because it didn't have a timezone, Java.Util.TimeZone.GetTimeZone was throwing an exception when I passed in an empty timezone identifier.

string calTzId = GetTimeZoneIdForCalendar(calId); // Evaluates to null
Java.Util.TimeZone jCalTz = Java.Util.TimeZone.GetTimeZone(calTzId); // Exception thrown here

The solution is to check if calTzId is null or empty and use the user's preferred timezone in the event that it is.

Java.Util.TimeZone jCalTz = !string.IsNullOrEmpty(calTzId)
    ? Java.Util.TimeZone.GetTimeZone(calTzId)
    : Java.Util.TimeZone.Default;

See Xamarin's Java.Util.TimeZone.Default documentation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM