2013-04-12 17 views
7

Sto tentando di aggiungere un gestore predefinito alla mia applicazione in modo da poter recuperare da eccezioni altrimenti non gestite.MonoDroid: recupero eccezioni non gestito

Ho trovato tre meccanismi forniti da Android/MonoDroid che, per quanto posso dire, dovrebbero renderlo possibile, ma non riesco a far funzionare nessuno di loro. Ecco il mio codice:

using System; 
using Android.App; 
using Android.Content; 
using Android.Runtime; 
using Android.Views; 
using Android.Widget; 
using Android.OS; 

namespace TestApp { 
    [Android.App.Activity(Label = "TestApp", MainLauncher = true, Icon = "@drawable/icon")] 
    public class MainActivity : Activity { 
     protected override void OnCreate(Bundle bundle) { 
      base.OnCreate(bundle); 
      SetContentView(new LinearLayout(this)); 

      //set up handlers for uncaught exceptions: 
      //Java solution 
      Java.Lang.Thread.DefaultUncaughtExceptionHandler = new ExceptionHandler(this); 
      //MonoDroid solution #1 
      AndroidEnvironment.UnhandledExceptionRaiser += AndroidEnvironment_UnhandledExceptionRaiser; 
      //MonoDroid solution #2 
      AppDomain.CurrentDomain.UnhandledException += delegate { new Alert(this, "AppDomain.CurrentDomain.UnhandledException", "error"); }; 

      //throw an exception to test 
      throw new Exception("uncaught exception"); 
     } 

     void AndroidEnvironment_UnhandledExceptionRaiser(object sender, RaiseThrowableEventArgs e) 
     { 
      //found a suggestion to set the Handled flag=true, but it has no effect 
      new Android.Runtime.RaiseThrowableEventArgs(e.Exception).Handled = true; 
      new Alert(this, "AndroidEnvironment.UnhandledExceptionRaiser", "error"); 
     } 
    } 

    public class ExceptionHandler : Java.Lang.Object, Java.Lang.Thread.IUncaughtExceptionHandler { 
     private Context _context; 
     public ExceptionHandler(Context c) { _context = c; } 
     public void UncaughtException(Java.Lang.Thread thread, Java.Lang.Throwable ex) { 
      new Alert(_context, "java exception handler"); 
     } 
    } 

    public class Alert { 
     public Alert(Context c, string src, string title = "alert") { 
      Android.App.AlertDialog.Builder builder = new Android.App.AlertDialog.Builder(c); 
      builder.SetTitle(title); 
      builder.SetMessage(src); 
      builder.SetPositiveButton("Ok", delegate { }); 
      Android.App.AlertDialog alert = builder.Create(); 
      alert.Show(); 
     } 
    } 
} 

Qualsiasi aiuto sarebbe apprezzato, grazie!

+0

Hai mai funzionato? Sto riscontrando problemi simili quando non eseguo il debug della mia applicazione. Posso dirti che il 'AndroidEnvironment' viene chiamato e gestisce le eccezioni non rilevate prima di' AppDomain'. –

+0

Questo non è il modo "per impostare il flag Handled = true", dovrebbe essere 'e.Handled = true;' ...: -B –

risposta

0

È possibile rilevare la maggior parte delle eccezioni nell'evento AppDomain.CurrentDomain.UnhandledException. Ma non è possibile chiamare AlertDialog in questo delegato, poiché: 1. L'applicazione si è arrestata in modo anomalo. 2. L'evento UnhandledException viene chiamato almeno. 3. alert.Show() viene eseguito async (in thread UI)

Quindi, è necessario utilizzare le operazioni di sincronizzazione (System.IO, ad esempio). Non dovresti utilizzare operazioni di interfaccia utente nativa, perché sono asincrone.

Problemi correlati