问题
I'm writing an Xamarin forms android application and implementing push notifications for the same. I am getting below mentioned stack-trace while calling ParseInstallation.SaveAsync() method in Android:
[MonoDroid] UNHANDLED EXCEPTION:
[MonoDroid] System.AggregateException: One or more errors occurred. ---> System.FormatException: String was not recognized as a valid DateTime.
[MonoDroid] at System.DateTimeParse.ParseExact (System.String s, System.String format, System.Globalization.DateTimeFormatInfo dtfi, DateTimeStyles style) [0x00027] in /Users/builder/data/lanes/2058/58099c53/source/mono/external/referencesource/mscorlib/system/globalization/datetimeparse.cs:57
[MonoDroid] at System.DateTime.ParseExact (System.String s, System.String format, IFormatProvider provider) [0x00000] in /Users/builder/data/lanes/2058/58099c53/source/mono/external/referencesource/mscorlib/system/datetime.cs:1083
[MonoDroid] at Parse.ParseClient.ParseDate (System.String input) [0x0000b] in :0
[MonoDroid] at Parse.ParseObject.MergeMagicFields (IDictionary
2 data) [0x000a4] in <filename unknown>:0
[MonoDroid] at Parse.ParseObject.MergeFromServer (IDictionary
2 data) [0x0001a] in :0
[MonoDroid] at Parse.ParseObject.MergeAfterSave (IDictionary
2 result) [0x0003a] in <filename unknown>:0
[MonoDroid] at Parse.ParseObject+<>c__DisplayClass2a.<SaveAsync>b__26 (System.Threading.Tasks.Task
1 t) [0x0004c] in :0
[MonoDroid] at System.Threading.Tasks.ContinuationResultTaskFromResultTask2[System.Tuple2[System.Net.HttpStatusCode,System.Collections.Generic.IDictionary2[System.String,System.Object]],System.Threading.Tasks.Task1[System.Tuple2[System.Net.HttpStatusCode,System.Collections.Generic.IDictionary2[System.String,System.Object]]]].InnerInvoke () [0x00027] in /Users/builder/data/lanes/2058/58099c53/source/mono/external/referencesource/mscorlib/system/threading/Tasks/TaskContinuation.cs:207
[MonoDroid] at System.Threading.Tasks.Task.Execute () [0x00016] in /Users/builder/data/lanes/2058/58099c53/source/mono/external/referencesource/mscorlib/system/threading/Tasks/Task.cs:2523
[MonoDroid] --- End of stack trace from previous location where exception was thrown ---
[MonoDroid] at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000b] in /Users/builder/data/lanes/2058/58099c53/source/mono/mcs/class/corlib/System.Runtime.ExceptionServices/ExceptionDispatchInfo.cs:61
[MonoDroid] at Parse.Internal.InternalExtensions+<>c__DisplayClass7
1[System.Object].<OnSuccess>b__6 (System.Threading.Tasks.Task t) [0x00033] in <filename unknown>:0
[MonoDroid] at System.Threading.Tasks.ContinuationResultTaskFromTask
1[System.Threading.Tasks.Task
1[System.Object]].InnerInvoke () [0x00027] in /Users/builder/data/lanes/2058/58099c53/source/mono/external/referencesource/mscorlib/system/threading/Tasks/TaskContinuation.cs:111
[MonoDroid] at System.Threading.Tasks.Task.Execute () [0x00016] in /Users/builder/data/lanes/2058/58099c53/source/mono/external/referencesource/mscorlib/system/threading/Tasks/Task.cs:2523
[MonoDroid] --- End of inner exception stack trace ---
[MonoDroid] at System.Threading.Tasks.Task.ThrowIfExceptional (bool) [0x00014] in /Users/builder/data/lanes/2058/58099c53/source/mono/external/referencesource/mscorlib/system/threading/Tasks/Task.cs:2168
[MonoDroid] at System.Threading.Tasks.Task.Wait (int,System.Threading.CancellationToken) [0x00052] in /Users/builder/data/lanes/2058/58099c53/source/mono/external/referencesource/mscorlib/system/threading/Tasks/Task.cs:3218
[MonoDroid] at System.Threading.Tasks.Task.Wait (int) [0x00000] in /Users/builder/data/lanes/2058/58099c53/source/mono/external/referencesource/mscorlib/system/threading/Tasks/Task.cs:3159
[MonoDroid] at Parse.ParsePushService.OnHandleIntent (Android.Content.Intent) <IL 0x00063, 0x0033b>
[MonoDroid] at Android.App.IntentService.n_OnHandleIntent_Landroid_content_Intent_ (intptr,intptr,intptr) [0x00011] in /Users/builder/data/lanes/2058/58099c53/source/monodroid/src/Mono.Android/platforms/android-22/src/generated/Android.App.IntentService.cs:141
[MonoDroid] at (wrapper dynamic-method) object.4f1ee6dc-1a44-45bb-ba65-81f69a0db824 (intptr,intptr,intptr) <IL 0x00017, 0x00043>
[MonoDroid] ---> (Inner Exception #0) System.FormatException: String was not recognized as a valid DateTime.
[MonoDroid] at System.DateTimeParse.ParseExact (System.String s, System.String format, System.Globalization.DateTimeFormatInfo dtfi, DateTimeStyles style) [0x00027] in /Users/builder/data/lanes/2058/58099c53/source/mono/external/referencesource/mscorlib/system/globalization/datetimeparse.cs:57
[MonoDroid] at System.DateTime.ParseExact (System.String s, System.String format, IFormatProvider provider) [0x00000] in /Users/builder/data/lanes/2058/58099c53/source/mono/external/referencesource/mscorlib/system/datetime.cs:1083
[MonoDroid] at Parse.ParseClient.ParseDate (System.String input) [0x0000b] in <filename unknown>:0
[MonoDroid] at Parse.ParseObject.MergeMagicFields (IDictionary
2 data) [0x000a4] in :0
[MonoDroid] at Parse.ParseObject.MergeFromServer (IDictionary
2 data) [0x0001a] in <filename unknown>:0
[MonoDroid] at Parse.ParseObject.MergeAfterSave (IDictionary
2 result) [0x0003a] in :0
[MonoDroid] at Parse.ParseObject+<>c__DisplayClass2a.b__26 (System.Threading.Tasks.Task
1 t) [0x0004c] in <filename unknown>:0
[MonoDroid] at System.Threading.Tasks.ContinuationResultTaskFromResultTask
2[System.Tuple2[System.Net.HttpStatusCode,System.Collections.Generic.IDictionary2[System.String,System.Object]],System.Threading.Tasks.Task1[System.Tuple2[System.Net.HttpStatusCode,System.Collections.Generic.IDictionary
2[System.String,System.Object]]]].InnerInvoke () [0x00027] in /Users/builder/data/lanes/2058/58099c53/source/mono/external/referencesource/mscorlib/system/threading/Tasks/TaskContinuation.cs:207
[MonoDroid] at System.Threading.Tasks.Task.Execute () [0x00016] in /Users/builder/data/lanes/2058/58099c53/source/mono/external/referencesource/mscorlib/system/threading/Tasks/Task.cs:2523
[MonoDroid] --- End of stack trace from previous location where exception was thrown ---
[MonoDroid] at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000b] in /Users/builder/data/lanes/2058/58099c53/source/mono/mcs/class/corlib/System.Runtime.ExceptionServices/ExceptionDispatchInfo.cs:61
[MonoDroid] at Parse.Internal.InternalExtensions+<>c__DisplayClass7
1[System.Object].b__6 (System.Threading.Tasks.Task t) [0x00033] in :0
[MonoDroid] at System.Threading.Tasks.ContinuationResultTaskFromTask1[System.Threading.Tasks.Task1[System.Object]].InnerInvoke () [0x00027] in /Users/builder/data/lanes/2058/58099c53/source/mono/external/referencesource/mscorlib/system/threading/Tasks/TaskContinuation.cs:111
[MonoDroid] at System.Threading.Tasks.Task.Execute () [0x00016] in /Users/builder/data/lanes/2058/58099c53/source/mono/external/referencesource/mscorlib/system/threading/Tasks/Task.cs:2523 <---
Mentioned below is my code :-
var installation = ParseInstallation.CurrentInstallation;
installation.AddUniqueToList ("channels", "RxLifeTime");
await installation.SaveAsync ();
if (installation != null) {
App.regId = System.String.IsNullOrWhiteSpace
(installation.DeviceToken) ? "" : installation.DeviceToken;
ParsePush.ParsePushNotificationReceived += PushNotificationReceived;
}
I'm facing this issue since last two months and it is very important for me to get it resolved. The same issue was reported on Google groups at below mentioned link :- Same Issue Here
Also some times I'm getting below mentioned stack-trace : -
[MonoDroid] UNHANDLED EXCEPTION:
[MonoDroid] System.NullReferenceException: Object reference not set to an instance of an object
[MonoDroid] at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000b] in /Users/builder/data/lanes/2058/58099c53/source/mono/mcs/class/corlib/System.Runtime.ExceptionServices/ExceptionDispatchInfo.cs:61
[MonoDroid] at System.Runtime.CompilerServices.AsyncMethodBuilderCore.m__0 (object) [0x00000] in /Users/builder/data/lanes/2058/58099c53/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:1006
[MonoDroid] at Android.App.SyncContext/c__AnonStorey0.<>m__0 () [0x00000] in /Users/builder/data/lanes/2058/58099c53/source/monodroid/src/Mono.Android/src/Android.App/SyncContext.cs:18
[MonoDroid] at Java.Lang.Thread/RunnableImplementor.Run () [0x0000b] in /Users/builder/data/lanes/2058/58099c53/source/monodroid/src/Mono.Android/src/Java.Lang/Thread.cs:36
[MonoDroid] at Java.Lang.IRunnableInvoker.n_Run (intptr,intptr) [0x00009] in /Users/builder/data/lanes/2058/58099c53/source/monodroid/src/Mono.Android/platforms/android-22/src/generated/Java.Lang.IRunnable.cs:71
[MonoDroid] at (wrapper dynamic-method) object.853c0f2f-07e2-447e-8d30-7ae1bda0b4ef (intptr,intptr)
[AndroidRuntime] Shutting down VM
回答1:
After few days of research I found an answer. We can use UnhandledExceptionRaiser provided by xamarin to avoid such unexpected crashes and this resolved the above mentioned issue. The same thing is mentioned here
Mentioned below is the code I'm using :
public class MainActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
// At the end of OnCreate
AndroidEnvironment.UnhandledExceptionRaiser += AndroidEnvironmentUnhandledExceptionRaiser;
}
private void AndroidEnvironmentUnhandledExceptionRaiser(object sender, RaiseThrowableEventArgs e)
{
// Place a breakpoint in this method to inspect e.Exception
e.Handled = true;
}
}
来源:https://stackoverflow.com/questions/32587891/parseinstallation-saveasync-causing-unhandledexception-on-xamarin-forms-androi