0
votes

I am trying to create a sample Xamarin.Android C# Application that uses Catel 4.5.4. It consists of an Activity, View Model class, and an Application class. (code at the end)

When I run it, I get the following error:

10-23 13:41:04.890 I/MonoDroid( 8769): System.NotSupportedException: Could not activate JNI Handle 0x6ec00025 (key_handle 0x41ed37d8) of Java type 'md5b251e24e9fff919bf177c55c6e30cd8e/MainActivity' as managed type 'Test.Views.MainActivity'. ---> System.NotSupportedException: To support navigation events in Android, Catel uses a custom ActivityLifecycleCallbacksListener. This requires an app instance though. Please make sure that the Android app contains an Application class.

Full Debug Log: https://pastebin.com/UZHhVHfL

The error seems to be coming from this line in Catel source code:

https://github.com/Catel/Catel/blob/hotfix/4.5.4/src/Catel.MVVM/Catel.MVVM.Shared/MVVM/Navigation/NavigationAdapter.phone.android.cs#L210

    var activity = GetNavigationTarget<Activity>();
        var application = activity.Application;
        if (application == null)
        {
            const string error = "To support navigation events in Android, Catel uses a custom ActivityLifecycleCallbacksListener. This requires an app instance though. Please make sure that the Android app contains an Application class.";
            Log.Error(error);

            -->throw new NotSupportedException(error);

Here the activity.Application is null. How do I fix this?


I have tried the code with the following Catel versions:

4.1.0 - Code works

4.2.0 - Code works

4.5.0 - Code works

4.5.1 - Error

4.5.4 - Error

I have to use 4.5.4 because after fixing this, I want to port an existing WPF+Catel(4.5.4) Windows Application to Android.


Here is the relevant parts of the code:

Views/MainActivity.cs

using Android.App;
using Android.OS;
using Android.Util;
namespace Test.Views
{
    [Activity(Label = "Test", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Catel.Android.App.Activity
    {

        protected override void OnCreate(Bundle bundle)
        {
            Log.Debug("MainActivity", "Activity onCreate Called");
            base.OnCreate(bundle);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);
        }
    }
}

MyApp.cs

I created this class because of the error, but it didn't solve the problem. Should I add anything in the constructor? Or override any other function?

using System;
using Android.App;
using Android.Runtime;
using Android.Util;


namespace Test
{
    [Application(Debuggable = true, Label = "Test App")]
    class MyApp : Catel.Android.App.Application
    {

        public MyApp(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
        {

        }


        public override void OnCreate()
        {
            base.OnCreate();
            Log.Error("MyApp", "Application onCreate Called");
        }
    }
}
1

1 Answers

0
votes
  1. We recommend to use catel 5.x, 4.x will not receive any new features and/or hotfixes. You could even go for Xamarin.Forms (which we are adding support for in 5.3 via .NET Standard 2.0).

  2. Make sure to actually instantiate the application class before the activity. Then it should work.