How to make this Callback Handler work?

philiphuynhphiliphuynh ✭✭USMember ✭✭
edited March 2017 in Xamarin.Android

I have the following code:
private Handler m_scraHandler = new Handler(new SCRAHandlerCallback());

private class SCRAHandlerCallback : Java.Lang.Object, Handler.ICallback
{
public IntPtr Handle
{
get;
private set;
}

        public void Dispose()
        {
        }

        public bool HandleMessage(Message msg)
        {
            return true;
        }

protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);

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

        MTSCRA mtscrea = new MTSCRA(this, m_scraHandler);
    }

For some reason when I run the application I would encounter the following error message:
System.NotSupportedException: Unable to activate instance of type MagTekDriver.MainActivity+SCRAHandlerCallback from native handle 0xfffe6f7c (key_handle 0x8bc78cc).

What is the cause of this error and how do I resolve it?

Best Answer

Answers

  • philiphuynhphiliphuynh ✭✭ USMember ✭✭

    Thank you so much for your help again Jon, that solved my issue.

  • mike004121mike004121 Member

    @JonDouglas said:
    If you inherit from Java.Lang.Object, you shouldn't have to add your own implementation of Handler. I would guess this is where the issue is happening.

    It should really look like this:

        public class MyHandler : Java.Lang.Object, Handler.ICallback
        {
            public bool HandleMessage(Message msg)
            {
                throw new NotImplementedException();
            }
        }
    

    Hi, @JonDouglas when I try this, I get a ClassNotFoundException for MyHandler. I can stop this exception from happening by putting [Register("<CallBack Class>")] however the application proceeds to crash when the call back is registered. The error is Fatal signal 6 (SIGABRT), code -6 in tid 4396. I have spent the whole day trying different things, but to no avail.

  • mike004121mike004121 Member

    Hi, I found my issue, I am using Xamarin Forms. I tried it in the Xamarin Android project and it worked fine.

Sign In or Register to comment.