No constructor found for ... (System.IntPtr, Android.Runtime.JniHandleOwnership)
Asked Answered
S

1

10

In the last few days I started getting the error above.
Not here and there, but everywhere. and in places I can't even put this weird constructor in, like the call-stack below.

I saw the answer in https://mcmap.net/q/342377/-monodroid-error-when-calling-constructor-of-custom-view-twodscrollview but I believe this is not my case. It simply started happening everwhere. especially when I put a breakpoint.

Here is an example:

10-26 15:34:58.895 E/mono-rt (13841): [ERROR] FATAL UNHANDLED EXCEPTION: System.NotSupportedException: Unable to activate instance of type Android.Views.View+IOnClickListenerImplementor from native handle 7960001d ---> System.MissingMethodException: No constructor found for Android.Views.View+IOnClickListenerImplementor::.ctor(System.IntPtr, Android.Runtime.JniHandleOwnership) ---> Java.Interop.JavaLocationException: Exception of type 'Java.Interop.JavaLocationException' was thrown.
10-26 15:34:58.895 E/mono-rt (13841): Java.Lang.Error: Exception of type 'Java.Lang.Error' was thrown.
10-26 15:34:58.895 E/mono-rt (13841): 
10-26 15:34:58.895 E/mono-rt (13841):   --- End of managed exception stack trace ---
10-26 15:34:58.895 E/mono-rt (13841): java.lang.Error: Java callstack:
10-26 15:34:58.895 E/mono-rt (13841):   at mono.android.view.View_OnClickListenerImplementor.n_onClick(Native Method)
10-26 15:34:58.895 E/mono-rt (13841):   at mono.android.view.View_OnClickListenerImplementor.onClick(View_OnClickListenerImplementor.java:29)
10-26 15:34:58.895 E/mono-rt (13841):   at android.view.View.performClick(View.java:4475)
10-26 15:34:58.895 E/mono-rt (13841):   at android.view.View$PerformClick.run(View.java:18786)
10-26 15:34:58.895 E/mono-rt (13841):   at android.os.Handler.handleCallback(Handler.java:730)
10-26 15:34:58.895 E/mono-rt (13841):   at android.os.Handler.dispatchMessage(Handler.java:92)
10-26 15:34:58.895 E/mono-rt (13841):   at android.os.Looper.loop
Stitching answered 26/10, 2014 at 13:43 Comment(1)
Have you found the solution?Hirsutism
A
10

From what I experienced, this can happens when an object is released from memory while your application is running. Then, for instance, if you go back to that page and the object needs to be recreated by Mono, you need to specify that constructor.

The John Pryor answer you are referring to should be the answer to your problem. The important part is the following :

So Mono for Android creates an instance of the appropriate type...via the (IntPtr, JniHandleOwnership) constructor, and generates an error if this constructor cannot be found.

Once the (in this case) TextView constructor finishes executing, the LogTextBox's ACW constructor will execute, at which point Mono for Android will go "aha! we've already created a C# instance for this Java instance", and will then invoke the appropriate constructor on the already created instance. Meaning that for a single instance, two constructors will be executed: the (IntPtr, JniHandleOwnership) constructor, and (later) the (Context, IAttributeSet, int) constructor.

I'd like to be a better help, but without any code snippet it's hard to tell. Try looking in object which implements the IOnClickListenerImplementorsee if you can add the constructor in the implementation of the listener.... Good luck

Anabelle answered 28/11, 2014 at 15:32 Comment(5)
3 years later, I am facing the problem. And this segment of doc from Xamarin site helped - Premature Dispose() Calls There is a mapping between a JNI handle and the corresponding C# instance. Java.Lang.Object.Dispose() breaks this mapping. If a JNI handle enters managed code after the mapping has been broken, it looks like Java Activation, and the (IntPtr, JniHandleOwnership) constructor will be checked for and invoked. If the constructor doesn't exist, then an exception will be thrown. developer.xamarin.com/guides/android/under_the_hood/…Blintze
FYI - I hit this too and learned that if this constructor is being sought after, something is very wrong. In my case, I had a local copy that somehow overrode the base version of the IntPtrHandle, like this: public IntPtr Handle { get; } ...removing that property allowed the map to work correctly. Thanks @Blintze for that great little tidbit.Graviton
Hi anyone did get solution for this. "Unable to activate instance of type Xamarin.Forms.Platform.Android.ListViewAdapter from native handle 0x7fd2839f64 (key_handle 0x1189fc)."Levitt
@PratiusDubey please open a bug on Xamarin.Forms GitHub issue trackerBurweed
Adding the missing constructor, helps me to solve my issue.Eliezer

© 2022 - 2024 — McMap. All rights reserved.