Skip to content Skip to sidebar Skip to footer

System.argumentexception'jobject' Must Not Be Intptr.zero. Parameter Name: Jobject

I get an error I cannot wrap my head around: I have this simple alert dialog builder in a void method private void startAction() { AlertDialog.Builder builder;

Solution 1:

The peer connection for AlertDialog ad; has been severed; while the object is still available in .NET (through the click handlers) it's Java counterpart has been collected. The binding between these two objects is held in a global reference stored in the IntPtr Handle property for all objects in .NET that implement IJavaObject.

When the cross VM collection cycle occurs, the Handle is set to IntPtr.Zero and the global Java reference is released to enable Dalvik (Java) to collect the Java object.

You're seeing this crash because the app may have been backgrounded and Android has triggered a collection on the apps process. This has resulted in most Java resources being destroyed by Dalviks garbage collector but their corresponding .NET objects are still alive and now point to an invalid Java object.

The fix for this is to check for the peer connection inside both the on-click handlers for the AlertDialog using the following code snippet:

publicstaticclassPeerConnectionHelper
{
    publicstaticboolHasPeerConnection(Java.Lang.Object jObj)
    {
        return !(jObj == null || jObj.Handle == System.IntPtr.Zero);
    }

    publicstaticboolHasPeerConnection (Android.Runtime.IJavaObject jObj)
    {
        return !(jObj == null || jObj.Handle == System.IntPtr.Zero);
    }
}

This would be implemented like so:

builder.SetPositiveButton ("OK", delegate { 
    if (!PeerConnectionHelper.HasPeerConnection(ad)) {
        return;
    }

    ad.Dismiss ();
    ShowDialog (0);
});
builder.SetNegativeButton ("Cancel", delegate { 
    if (!PeerConnectionHelper.HasPeerConnection(ad)) {
        return;
    }

    ad.Cancel ();
});

For some more reading:

Post a Comment for "System.argumentexception'jobject' Must Not Be Intptr.zero. Parameter Name: Jobject"