Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Application child class in Mono for Android

I'm trying to create a child class of "Android.App.Application" to override "OnCreate()", but I can't get it working. Here's my code:

namespace MonoAndroidAcra {
  [Application(Debuggable=true, 
               Label="insert label here",
               ManageSpaceActivity = typeof(MainActivity))]
  class AcraApp : Application {
    public override void OnCreate() {
      base.OnCreate();
    }
  }
}

MainActivity is just the default example activity.

Now, when I debug the project I get a System.NotSupportedException:

Unable to activate instance of type MonoAndroidAcra.AcraApp from native handle 405191a0

No call stack is available for this exception.

How do I do this correctly? I couldn't find any examples for this.

I'm using the latest stable version of Mono for Android.

like image 946
Sebastian Krysmanski Avatar asked Mar 29 '12 15:03

Sebastian Krysmanski


2 Answers

You need to add this constructor to your class in order to make it work:

public AcraApp (IntPtr javaReference, JniHandleOwnership transfer)
    : base(javaReference, transfer)
{
}
like image 131
Greg Shackles Avatar answered Oct 21 '22 23:10

Greg Shackles


This was a "leaky abstraction", explained by a Xamarin Team member, that have been fixed in the latest Xamarin Android Version 4.12.2, in which it is not required anymore to add this missing constructor. The Android SDK 22.6 is also required to properly function with this Xamarin version. However updating the SDK only will not fix this problem, this is really Xamarin related.

For anyone using older version, Greg's solution should still be used.

like image 24
ForceMagic Avatar answered Oct 21 '22 23:10

ForceMagic