11

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.

Sebastian Krysmanski
  • 8,114
  • 10
  • 49
  • 91

2 Answers2

27

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

public AcraApp (IntPtr javaReference, JniHandleOwnership transfer)
    : base(javaReference, transfer)
{
}
Greg Shackles
  • 10,009
  • 2
  • 29
  • 35
1

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.

Community
  • 1
  • 1
ForceMagic
  • 6,230
  • 12
  • 66
  • 88
  • It seems that [@GregShackles answer](http://stackoverflow.com/a/9963637/121968) is required even for 6.1.0.71. – TN. Aug 18 '16 at 14:14
  • @TN. Good to know thanks for pointing that out, I wonder if it's a regression issue that just came back in. I haven't been using Xamarin for about 2 years now. – ForceMagic Aug 18 '16 at 14:55