System.ArgumentException'jobject 'should not be IntPtr.Zero. Parameter Name: jobject

I get an error message that I cannot wrap around:

I have this simple dialog alert builder in the void method

private void startAction() {
                AlertDialog.Builder builder;
                builder = new AlertDialog.Builder (this);
                var ad = builder.Create ();
                builder.SetMessage ("Some text");
                builder.SetPositiveButton ("OK", delegate { 
                    ad.Dismiss ();
                    ShowDialog (0);
                });
                builder.SetNegativeButton ("Cancel", delegate { 
                    ad.Cancel ();
                });
                builder.SetCancelable (true);                    
                builder.Show ();
}

Xamarin Insights showed me a crash report (several times) that I cannot reproduce or understand.

System.ArgumentException'jobject' must not be IntPtr.Zero. Parameter name: jobject
Raw
Android.Runtime.JNIEnv.CallVoidMethod(IntPtr jobject, IntPtr jmethod)
Android.App.Dialog.Dismiss()
SpoonacularApp.Droid.ShoppingListActivity.<startAction>c__AnonStorey3.<>m__0(object, DialogClickEventArgs)
Android.Content.IDialogInterfaceOnClickListenerImplementor.OnClick(IDialogInterface dialog, int which)
Android.Content.IDialogInterfaceOnClickListenerInvoker.n_OnClick_Landroid_content_DialogInterface_I(IntPtr jnienv, IntPtr native__this, IntPtr native_dialog, int which)
at (wrapper dynamic-method) System.Object:ba5962df-899a-46fd-a4bd-6c9ffe426b75 (intptr,intptr,intptr,int)

Which argument refers to this exception?

I got the same error message with Android.App.Dialog.Cancel()instead Android.App.Dialog.Dismiss().

+4
source share
1 answer

AlertDialog ad; ; - .NET( ), Java-. , IntPtr Handle .NET, IJavaObject.

-VM, Handle IntPtr.Zero, Java , Dalvik (Java) Java.

, , Android . , Java Dalviks, .NET Java.

- AlertDialog :

public static class PeerConnectionHelper
{
    public static bool HasPeerConnection(Java.Lang.Object jObj)
    {
        return !(jObj == null || jObj.Handle == System.IntPtr.Zero);
    }

    public static bool HasPeerConnection (Android.Runtime.IJavaObject jObj)
    {
        return !(jObj == null || jObj.Handle == System.IntPtr.Zero);
    }
}

:

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

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

    ad.Cancel ();
});

:

+6

Source: https://habr.com/ru/post/1622552/


All Articles