ParseInstallation.SaveAsync()在Xamarin表单Android上导致UnhandledException

时间:2015-09-15 13:51:07

标签: android parse-platform xamarin xamarin.android xamarin.forms

我正在编写一个Xamarin表单Android应用程序并实现推送通知。 我在Android中调用ParseInstallation.SaveAsync()方法时得到下面提到的堆栈跟踪:

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;
}

下面提到的是我的代码: -

[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

自从过去两个月以来我一直面对这个问题,对我来说解决这个问题非常重要。 Google群组在以下链接中报告了同样的问题: - Same Issue Here

有时我会得到下面提到的堆栈跟踪: -

 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v001="http://xmlns.tel.be/nin/v001">
   <soapenv:Header/>
   <soapenv:Body>
      <v001:Request>
         <v001:locationId>lyuda_0309001_loc</v001:locationId>
         <v001:workOrderId>88155530</v001:workOrderId>
         <v001:systemName>NINAS</v001:systemName>
         <v001:scenarioId>2</v001:scenarioId>
         <!--Optional:-->
         <v001:scenarioData>
            <v001:scenarioData></v001:scenarioData>
         </v001:scenarioData>
      </v001:Request>
   </soapenv:Body>
</soapenv:Envelope>

1 个答案:

答案 0 :(得分:1)

经过几天的研究,我找到了答案。 我们可以使用xamarin提供的UnhandledExceptionRaiser来避免这种意外崩溃,这解决了上述问题。同样的事情被提到here

下面提到的是我使用的代码:

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;
  }
}