What is the recipe to have a working Application-derived class ?

softlionsoftlion Benjamin MayrargueFRBeta ✭✭✭

Hi,
I've created a class derived from Android.App.Application, decorated the class with [Application], verified the class typename generated in application element of the manifest.xml file.
But when run, the application throws this exception:

System.NotSupportedException: Unable to activate instance of type Droid.Startup.MainApp from native handle 5c100019

What am i doing wrong ?

[Application]
public class MainApp : Application
{
    public override void OnCreate()
    {
        base.OnCreate();
        //app init ...
    }
}

Posts

  • JamesLaveryJamesLavery James Lavery GBBeta, University ✭✭✭

    Our posts crossed...

    Not so sure about needing two constructors or all lowercase. Ours works as implemented in the code I posted!

  • xceedxceed Iain Collins GBMember

    Hi all, thanks for the code posted above @JamesLavery‌, it's working for me. Interestingly I have some Console.WriteLine() statements in my MainActivity and I see them output before the WriteLine() call I have in my App class.

    I just wanted to check one thing, I intend to use my App base class to store some Dictionarys that map values that I send to the cloud. The only way I can think of doing this is to make the base App class static and then access the dictionaries like so: App.CompanyNames[x]. Is this the best/correct way?

    I also make use of a "ShowDialogue" (which uses AlertDialog) function across some of my activities, rather than have this function in all my activity classes, I thought about putting it in the App class, I've not tried it but does that sound like a good move? Or should I be trying to override an Activity class somewhere so I only have one method for ShowDialogue and not need to duplicate it everywhere?

  • JamesRocheJamesRoche James Roche GBMember

    Mine didn't work without the override of OnCreate(), just for anyone having trouble.

  • tonyjohntonyjohn tony john USMember

    Thanks @JamesLavery !!! Your post about overriding the constructor really helped!!

  • vsg24vsg24 Vahid Amiri Motlagh TRMember ✭✭

    Worked for me even without OnCreate()

  • RyanHaworthRyanHaworth Ryan Haworth USMember
    edited April 2016

    Has this been fixed? I get an exception when the OnCreate method is trying to be called. This is quite an important class and is anyone seeing this situation. I am hoping someone will now what the excpetion means and what configuration I need. I checked my Manfest file and the application name is being set by the class Attribute.

    `namespace Sonr.Android
    {
    [Application]
    public class OrcasApplication : Application
    {
    public OrcasApplication()
    {
    }

        protected OrcasApplication(IntPtr javaReference, JniHandleOwnership transfer)
            : base(javaReference, transfer)
        {
        }
    
        public override void OnCreate()
        {
            base.OnCreate();
    
            UnityService.UnitySetup();
        }
    }
    

    }`

    The exception:

    Unhandled Exception:

    Java.Lang.UnsatisfiedLinkError: No implementation found for void md5116445eeb8f890e5d6a7544569995520.OrcasApplication.n_onCreate() (tried Java_md5116445eeb8f890e5d6a7544569995520_OrcasApplication_n_1onCreate and Java_md5116445eeb8f890e5d6a7544569995520_OrcasApplication_n_1onCreate__)

    Thank you for any help.

  • Umit2AYDINUmit2AYDIN Umit AYDIN AUMember

    work> @softlion said:

    Well, 3 minutes later, got it working ok !

    You must add 1 non default constructor, the same as the one in the Application class.

     [Application]
    public class MainApp : Application
    {
        public MainApp(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
        {
        }
    
        public override void OnCreate()
        {
            base.OnCreate();
            //app init ...
        }
    }
    

    EDIT : @JamesLavery, the lower case letters bug was caused by the temporary files left by the compiler when the constructor was missing. A clean / rebuild and it accepts the pascal-cased class name again.

    @JamesLavery : initializing in OnCreate let you use the applicationcontext, and call virtual methods, which is prohibited/bad practice in the constructor.

    It worked with me :smile:

  • rbmanian75rbmanian75 Balasubramanian Ramanathan USMember ✭✭

    The application attribute seems to be not respected with xamarin studio 6?. It was working with previous version. Any idea how to fix it with xamarin studio 6?

  • XasanrahXasanrah Xasan rah CZMember ✭✭

    Hi Ryan Haworth, Did you solve the issue, I have the same

  • XasanrahXasanrah Xasan rah CZMember ✭✭

    @RyanHaworth said:
    Has this been fixed? I get an exception when the OnCreate method is trying to be called. This is quite an important class and is anyone seeing this situation. I am hoping someone will now what the excpetion means and what configuration I need. I checked my Manfest file and the application name is being set by the class Attribute.

    `namespace Sonr.Android
    {
    [Application]
    public class OrcasApplication : Application
    {
    public OrcasApplication()
    {
    }

        protected OrcasApplication(IntPtr javaReference, JniHandleOwnership transfer)
            : base(javaReference, transfer)
        {
        }
    
        public override void OnCreate()
        {
            base.OnCreate();
    
            UnityService.UnitySetup();
        }
    }
    

    }`

    The exception:

    Unhandled Exception:

    Java.Lang.UnsatisfiedLinkError: No implementation found for void md5116445eeb8f890e5d6a7544569995520.OrcasApplication.n_onCreate() (tried Java_md5116445eeb8f890e5d6a7544569995520_OrcasApplication_n_1onCreate and Java_md5116445eeb8f890e5d6a7544569995520_OrcasApplication_n_1onCreate__)

    Thank you for any help.

    Did you solve the issue, I have the same

Sign In or Register to comment.