xamarin / Xamarin.Forms

Xamarin.Forms is no longer supported. Migrate your apps to .NET MAUI.
https://aka.ms/xamarin-upgrade
Other
5.63k stars 1.88k forks source link

[Bug] Unable to activate instance of type Xamarin.Forms.Platform.Android.Platform+DefaultRenderer #12867

Open rubiomiguel06 opened 3 years ago

rubiomiguel06 commented 3 years ago

Description

I created a custom control called DragAndSwapContainer which implements two custom behaviors: DragBehavior and DropBehavior.

DragBehavior basically adds a PanGestureRecognizer to the View and emits an event when the View is being 'dragged' (and also translates the view according to the Pan done by the user). On the other side, DropBehavior listens to this events and emits other events when a View is passing over the container or when a View is dropped inside.

DragAndSwapContainer acts as a container that can receive other views, and as a draggable and droppable view at the same time. The goal of this container is to enable the user to swap between two views by dragging and dropping one into the other one.

When a View is dropped inside this Container what I do is, basically, to swap the Children Views that both containers have (the one with the view that was dragged, and the one receiving the dropped view). Here's a piece of code for the DragAndSwapContainer (which inherits from Grid):

private void OnViewDroppedInside(object sender, DroppedEventArgs e)
{
    var draggedViewContainer = e.DroppedView.Parent as DragAndSwapContainer;

    draggedViewContainer.Children.Add(this.Children.First());
    this.Children.Add(e.DroppedView);
}

Since each View can have only one parent, this works fine: Child View is exchanged by the DragAndSwapContainers.

Everything works fine as respects to iOS and Windows, but I am facing some problems with Android.

I am getting the following error https://github.com/xamarin/Xamarin.Forms/pull/8888 when swapping two views. After some debugging I conclude that the problem arises because while swapping the children Android native renderers are disposed, so I lose track of the View.

Also it is something that doesn't happen always, seems that there's some timings issues. If I attach my Analytics service (which reports to a cloud server and adds some overhead) the issue happens way more often.

The error mentioned seems to be fixed in the Xamarin.Forms version I have (since it was merged already in v4.4.0) but I'm still having the exact same error call stacks.

I managed to recreate this problem in a separate project (see attached) but only if using UXCam Analytics.. Unfortunately I couldn't reproduce it (at least in a fast reproducible way) without the use of UXCam, and I'm unable to share the AppKey I use to start a UXCam session through here.

Here's the error I get when the app crashes:

android.runtime.JavaProxyThrowable System.NotSupportedException: Unable to activate instance of type Xamarin.Forms.Platform.Android.Platform+DefaultRenderer from native handle 0x78115f90c4 (key_handle 0x87c6c2d). ---> System.MissingMethodException: No constructor found for Xamarin.Forms.Platform.Android.Platform+DefaultRenderer::.ctor(System.IntPtr, Android.Runtime.JniHandleOwnership) ---> Java.Interop.JavaLocationException: Exception of type 'Java.Interop.JavaLocationException' was thrown. --- End of inner exception stack trace --- at Java.Interop.TypeManager.CreateProxy (System.Type type, System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer) [0x000b5] in <2df86f44c8e946618f31cb3aed952f5b>:0 at Java.Interop.TypeManager.CreateInstance (System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer, System.Type targetType) [0x00111] in <2df86f44c8e946618f31cb3aed952f5b>:0 --- End of inner exception stack trace --- at Java.Interop.TypeManager.CreateInstance (System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer, System.Type targetType) [0x0017e] in <2df86f44c8e946618f31cb3aed952f5b>:0 at Java.Lang.Object.GetObject (System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer, System.Type type) [0x00023] in <2df86f44c8e946618f31cb3aed952f5b>:0 at Java.Lang.Object._GetObject[T] (System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer) [0x00017] in <2df86f44c8e946618f31cb3aed952f5b>:0 at Java.Lang.Object.GetObject[T] (System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer) [0x00000] in <2df86f44c8e946618f31cb3aed952f5b>:0 at Java.Lang.Object.GetObject[T] (System.IntPtr jnienv, System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer) [0x00006] in <2df86f44c8e946618f31cb3aed952f5b>:0 at Android.Views.View.n_SetOnTouchListener_Landroid_view_ViewOnTouchListener (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_l) [0x00000] in <2df86f44c8e946618f31cb3aed952f5b>:0 at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.37(intptr,intptr,intptr)

Steps to Reproduce

  1. Run the sample project on Android (with a UXCam AppKey inserted at UXCamAnalytics.cs)
  2. Start dragging and dropping views
  3. Eventually App crashes

Expected Behavior

App shouldn't crash and should work as it does on UWP and iOS

Basic Information

Reproduction Link

DragAndDrop.zip

Screenshots

image

PureWeen commented 3 years ago

@rubiomiguel06 I've been moving these cats around for awhile now and I'm not getting any crashes

I'm testing on an S9 and not seeing anything. Do you see this issue on any simulator versions? Can you try the latest 5.0 prerelease to see if you are having issues there?

rubiomiguel06 commented 3 years ago

@PureWeen thanks for trying to get the error. Unfortunately I'm just starting my vacation days, so until I come back I won't be able to test with the latest 5.0 prerelease. But please, don't close this issue for inactivity. This will be the first thing I'll focus on as soon as I am back. Anyway, did you try it using UXCam? I'll also try to make a sample where the use of UXCam is not needed, since it makes it difficult to test.

rubiomiguel06 commented 3 years ago

Hi @PureWeen, I've tested the latest 5.0 release and the problem persists.

One thing I've just found out is that the exception occurs only for Android 10 devices (or at least I wasn't able to reproduce it on other Android versions, excepting Android 11 which I could not test yet because I don't have a device or simulator with this version).

Please try the example I attach below where I added a UXCam key and updated the Xamarin.Forms version to the latest available one (v5.0.0.1874). Running it on Debug mode on an Android 10 device or simulator (connected to internet, so UXCam works), after some drags and drops, the exception should arise.

I've tested on the Android simulator Pixel 2 Q 10.0 - API 29 with the expected results (exception occurring after moving containers around).

DragAndDrop.zip

jsuarezruiz commented 3 years ago

Thanks for the attached repro sample!. I have not managed to get the crash dragging several times in Android simulator Pixel 2 Q 10.0 - API 29. @PureWeen Could you try the attached repro sample?

rubiomiguel06 commented 3 years ago

@jsuarezruiz thanks for trying to reproduce it on your side! One question, did you run the sample on Debug mode and with an internet connection? I ask you because I didn't get any new sessions on the UXCam analytics dashboard.

rmarinho commented 3 years ago

Hi @rubiomiguel06 i tried to reproduce and frag and drop the cards a lot of times and i wasn't able to make it crash. Anything special ? tested Pixel 2 simulator API 29

rubiomiguel06 commented 3 years ago

Hi @rmarinho, thanks for giving it a try. I ask you the same I did to @jsuarezruiz : did you run the sample on Debug mode and with an internet connection? I haven't seen any activity log on the UXCam analytics. I attach a video showing the crash I get.

The steps are:

  1. Run the sample (this) on Debug Mode.
  2. Check there's a working internet connection on the device.
  3. Drag and Drop cards. It might be that doing it fast makes it crash more often.

https://user-images.githubusercontent.com/19507731/110439171-6c57dc80-80b7-11eb-8d4c-7d2f5cee88c7.mp4

rachelkang commented 3 years ago

Hi, @rubiomiguel06 - thanks for elaborating. Confirming that I'm able to reproduce this on XF 5.0 on visual studio. Interestingly (although perhaps irrelevant), I was unable to reproduce the issue on VSMac. As you mentioned, I do think this may be a performance issue related to the speed and # of times in which the drag-and-drop is performed

Anup-UXCam commented 2 years ago

Hi, we've had a customer come to us with this particular issue. The stack traces are pointing to the setting of the onTouchListener. Any updates on this? Thanks!

MitchBomcanhao commented 1 year ago

I'm now running into a similar issue too

rubiomiguel06 commented 1 year ago

This ended up being an issue related directly with UXCam Android. They have fixed it and the issue is gone. I will close it

MitchBomcanhao commented 1 year ago

I'm still getting a similar issue on a page that contains a Listview, and some users one some devices seem to be able to trigger the issue repeatedly, but I've not been able to do it on any device I've tried so far. The google play store automated test does trigger this failure on one of their test devices (but not always), and viewing the video and trying to reproduce their interaction yelded no results :( there's no drag and drop, no fancy behaviours, just a list that can be tapped on to call a command.

Anup-UXCam commented 1 year ago

This is not an issue related with UXCam Android, the SDK is just adding a onTouchListener to views and it throws this exception. To avoid crashing apps we have disabled one of our features on the latest version of our Xamarin SDK. This is still a Xamarin issue, the views should not be throwing this exception on a simple onTouchListener.

MitchBomcanhao commented 1 year ago

I think I may have reproduced the app hanging/crashing on my own device (very little details on what is actually going on). The trick was going onto the accessibility settings on my android 11 device toggling "Remove animations" to ON. Then, when I'm triggering an action which will dismiss my current page (built with rg plugin popup) the app stops working with the dreaded "unable to activate instance" error. So, disabling animations makes things go bang...

MitchBomcanhao commented 1 year ago

ok, a bit more information - it seems that the popup page goes bang when animations are disabled and you replace the contents of the page, eg setting this.Content to a blank grid (or whatever you may want, we had an activity indicator) made it explode. this then bubbles up to the default renderer constructor failure that ultimately takes down the application.