Xamarin.forms: [Bug] System.NotSupportedException: Unable to activate instance of type Xamarin.Forms.Platform.Android.FastRenderers.LabelRenderer

Created on 27 Oct 2019  路  44Comments  路  Source: xamarin/Xamarin.Forms

Description

The following exception occurs when scrolling in the repro on an Android device.

**System.NotSupportedException:** 'Unable to activate instance of type Xamarin.Forms.Platform.Android.FastRenderers.LabelRenderer from native handle 0xbedad7ec (key_handle 0xd22eb7f).'

Steps to Reproduce

  1. Paste into MainPage.xaml.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace FastRendererCrash
{
    static class Extensions
    {
        public static View WithBounds(this View v, double x, double y, double w, double h)
        {
            AbsoluteLayout.SetLayoutBounds(v, new Rectangle(x, y, w, h));
            return v;
        }
    }
    // Learn more about making custom code visible in the Xamarin.Forms previewer
    // by visiting https://aka.ms/xamarinforms-previewer
    [DesignTimeVisible(false)]
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
            IEnumerable<View> Select((string groupHeader, IEnumerable<int> items) t)
            {
                yield return new AbsoluteLayout
                {
                    Children = {
                        new Label {
                            Text = t.groupHeader, HorizontalTextAlignment = TextAlignment.Center,
                            TextColor = Color.FromUint(0xff5a5a5a), FontSize = 10
                        }.WithBounds(0, 21.1, 310, AbsoluteLayout.AutoSize) },
                    HorizontalOptions = LayoutOptions.FillAndExpand,
                    VerticalOptions = LayoutOptions.Start,
                    HeightRequest = 46
                };
                foreach (var item in t.items)
                {
                    yield return new AbsoluteLayout
                    {
                        Children = {
                            new Image { Source = ImageSource.FromResource("FastRendererScrollCrash.light.png", System.Reflection.Assembly.GetCallingAssembly()) }
                            .WithBounds(23.6, 14.5, 14.9, 20.7),
                            new Label { Text = item.ToString(), TextColor = Color.FromUint(0xff5a5a5a), FontSize = 10 }
                            .WithBounds(58, 18.2, AbsoluteLayout.AutoSize, AbsoluteLayout.AutoSize)
                        },
                        HorizontalOptions = LayoutOptions.FillAndExpand,
                        VerticalOptions = LayoutOptions.Start,
                        HeightRequest = 49.7
                    };
                }
            }
            Content = new AbsoluteLayout
            {
                Children = {
                    new CollectionView {
                        ItemsSource =
                            new (string, Func<int, bool>)[] {
                                ("odd", i => i % 2 == 1),
                                ("even", i => i % 2 == 0),
                                ("triple", i => i % 3 == 0),
                                ("fives", i => i % 5 == 0) }
                            .Select(t => (t.Item1, Enumerable.Range(1, 100).Where(t.Item2)))
                            .SelectMany(Select),
                        ItemTemplate = new DataTemplate(() => {
                            var template = new ContentView();
                            template.SetBinding(ContentView.ContentProperty, ".");
                            return template;
                        }),
                        ItemsLayout = LinearItemsLayout.Vertical,
                        ItemSizingStrategy = ItemSizingStrategy.MeasureFirstItem
                    }
                }
            };
        }
    }
}
  1. Add an embedded image file with name "light.png"
  2. Debug the app onto an Android phone (e.g. my Xiaomi Redmi 6 running Android 8.1 is a phone that can repro the exception)
  3. Scroll downwards quickly (aim for at least 30 items per screen update)
  4. After 2 screen updates, the app crashes:
10-28 01:20:31.487 I/MonoDroid(17924): UNHANDLED EXCEPTION:
10-28 01:20:31.491 I/MonoDroid(17924): System.NotSupportedException: Unable to activate instance of type Xamarin.Forms.Platform.Android.FastRenderers.LabelRenderer from native handle 0xbedad7ec (key_handle 0xf313e4). ---> System.MissingMethodException: No constructor found for Xamarin.Forms.Platform.Android.FastRenderers.LabelRenderer::.ctor(System.IntPtr, Android.Runtime.JniHandleOwnership) ---> Java.Interop.JavaLocationException: Exception of type 'Java.Interop.JavaLocationException' was thrown.
10-28 01:20:31.491 I/MonoDroid(17924):    --- End of inner exception stack trace ---
10-28 01:20:31.491 I/MonoDroid(17924):   at Java.Interop.TypeManager.CreateProxy (System.Type type, System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer) [0x00055] in <11a340ccc8de43f09c97400139266ef5>:0 
10-28 01:20:31.491 I/MonoDroid(17924):   at Java.Interop.TypeManager.CreateInstance (System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer, System.Type targetType) [0x00116] in <11a340ccc8de43f09c97400139266ef5>:0 
10-28 01:20:31.491 I/MonoDroid(17924):    --- End of inner exception stack trace ---
10-28 01:20:31.492 I/MonoDroid(17924):   at Java.Interop.TypeManager.CreateInstance (System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer, System.Type targetType) [0x00182] in <11a340ccc8de43f09c97400139266ef5>:0 
10-28 01:20:31.492 I/MonoDroid(17924):   at Java.Lang.Object.GetObject (System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer, System.Type type) [0x000c1] in <11a340ccc8de43f09c97400139266ef5>:0 
10-28 01:20:31.492 I/MonoDroid(17924):   at Java.Lang.Object._GetObject[T] (System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer) [0x00017] in <11a340ccc8de43f09c97400139266ef5>:0 
10-28 01:20:31.492 I/MonoDroid(17924):   at Java.Lang.Object.GetObject[T] (System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer) [0x00000] in <11a340ccc8de43f09c97400139266ef5>:0 
10-28 01:20:31.492 I/MonoDroid(17924):   at Java.Lang.Object.GetObject[T] (System.IntPtr jnienv, System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer) [0x00006] in <11a340ccc8de43f09c97400139266ef5>:0 
10-28 01:20:31.492 I/MonoDroid(17924):   at Android.Views.View.n_Invalidate (System.IntPtr jnienv, System.IntPtr native__this) [0x00000] in <11a340ccc8de43f09c97400139266ef5>:0 
10-28 01:20:31.492 I/MonoDroid(17924):   at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.55(intptr,intptr)

Expected Behavior

The app does not crash and continues to scroll.

Actual Behavior

The app crashes.

Basic Information

  • Version with issue: 4.2, 4.3.0.908765
  • Last known good version: Idk
  • IDE: VS16.3.6
  • Platform Target Frameworks:

    • Android: 9.0

  • Android Support Library Version: 28.0.0.3
  • Nuget Packages: XForms4.3.0.908765, XEssentials1.3.1, NetStandard2.0.3, AndroidSupport28.0.0.3
  • Affected Devices: Android phones including Xiaomi Redmi 6

Screenshots

During step 4:

image|image
------|----------
image|image

Reproduction Link


FastRendererCrash.zip

5 in-progress Android bug

Most helpful comment

I found a temporary fix:

I added second constructor which is obsolete to custom renderer. Now after scrolling this constructor is hit, and no errors appear.

[assembly: ExportRenderer(typeof(Label), typeof(CustomLabelRenderer))]
namespace MyApp.Droid.Renderers
{
public class CustomLabelRenderer : Xamarin.Forms.Platform.Android.FastRenderers.LabelRenderer
{
public CustomLabelRenderer(Context context) : base(context)
{

    }

    [Obsolete]
    public CustomLabelRenderer(IntPtr handle, JniHandleOwnership transfer)
    {

    }
}

}

All 44 comments

Related: #2444, #7663

I have the same issue.
I have listview, with labels on it. When I do few scrolls up-down, I see this crash..
When do you plan to fix it?

I found a temporary fix:

I added second constructor which is obsolete to custom renderer. Now after scrolling this constructor is hit, and no errors appear.

[assembly: ExportRenderer(typeof(Label), typeof(CustomLabelRenderer))]
namespace MyApp.Droid.Renderers
{
public class CustomLabelRenderer : Xamarin.Forms.Platform.Android.FastRenderers.LabelRenderer
{
public CustomLabelRenderer(Context context) : base(context)
{

    }

    [Obsolete]
    public CustomLabelRenderer(IntPtr handle, JniHandleOwnership transfer)
    {

    }
}

}

@Happypig375 Thanks for your reproduction case.
I reproduce the issue, I try to identify the root cause, it's not simple because it an issue triggered by the interaction with the native android part of code.
I have some ideas to identify the cause, but I cannot provide you a resolution date.
I will report here later if I made some progress on this.

+1

En mi caso falla cuando hago modificaciones sobre el XAML con HotReload activo.

I have been able to work on this lately.
I identified the root cause which affect the reproduction app.
I found a solution which seem to fix the issue but I'm not sure if it is the right way to fix the issue at the moment.
I need more time to confirm that and test other approach.

Here a nupkg version to test the modification. The zip extension need to replaced by nupkg (github limit file upload extension).
Xamarin.Forms.4.3.99.zip

Was going to test out your fix but unfortunately my project is affected by this https://github.com/reactiveui/Pharmacist/issues/77 that stops me from trialling it.

@rbev You have two ways to install it.
Use a private feed like AzureDevOps Artifacts, MyGet or other ...
Or you can install the feed from a local path, nuget source can be either http url or directory path.

Installing it from a local feed was fine, however our build process requires Pharmacist to run and it has an open bug on not supporting private feeds.

@kvpt i figured out a way to test out your build and I've been unable to replicate the issue while using your build

While my case wasn't a 100% replication scenario I was previously able to replicate it at least once every half dozen or so test runs on debug builds.

I eagerly await your PR @kvpt :-)

Is there any workaround for this that doesn't require using legacy renderers? I can get by on legacy but the performance throughout my app is noticeably worse while I wait.

The custom renderer posted by @school-today works, even if it's ugly.

@PureWeen PR done.

@samhouts - It reproduces also when dynamically changing the size of Label. Please put a high priority and fix this ASAP.

+1. Unfortunately also seeing this

+1 on Xamarin.Forms 4.4.0.991265

Confirming for 4.4

@EmilAlipiev (deleted comment):

wow 2 months already and no action on such vital issue.

Complaining is no use.. I tried this in another similarly critical issue but Xamarin's way of dealing with customer anger is to mark them as off-topic.

Still fails - very disappointing since workarounds do NOT work.

@kvpt were you able to reproduce the dispose exception on the Label?

Using the reproduction attached to this issue
https://github.com/xamarin/Xamarin.Forms/issues/8262#issue-512996294

The exception I'm seeing is from the ImageRenderer.
I created this PR which seems to fix the ImageRenderer scenario
https://github.com/xamarin/Xamarin.Forms/pull/9034

@PureWeen The issue (like almost others related to dispose) is timing related.
On the reproduction case, on my computer I have 80% of the time a crash from the LabelRenderer and 20% of the time a crash from the DefaultRenderer (from the layout).
And each time I see in the stacktrace that the issue is triggered by a view invalidation triggered by an animation.
All the details are in my PR description.

So I think the only fix in this case is to delay the dispose after the animation is over.

However, adding more dispose check will perhaps fix others issues related dispose, so your PR is useful too.

@kvpt Ok now I'm seeing the Labelrenderer one now.

I was only getting the ImageRenderer one but now that I have that resolved the LabelRenderer one is popping up which your PR looks to fix.

@kvpt interesting observation I've asked the android team about

If you just remove this override
https://github.com/xamarin/Xamarin.Forms/blob/bd31e1e9fc8b2f9ad94cc99e0c7ab058174821f3/Xamarin.Forms.Platform.Android/Renderers/FormsTextView.cs#L29

The exception goes away.

@PureWeen I'm not sure if it is the override of the invalidate method or the skipping of the invalidation which cause a delay to it because android do it later anyway.

There is really an issue with the invalidation timing which seem to happen sometime after the dispose, with the collection view at least.
The problem doesn't seem specific to a renderer type, although the label renderer are the more impacted by the issue.

If I remove the skip invalidate, the issue is impacting the renderer of the layout, but is much harder to reproduce.

image
image

@kvpt if you remove all overrides of Invalidate do you still get an exception?

Like if you use the code I have in this PR?

https://github.com/xamarin/Xamarin.Forms/pull/9034

@PureWeen I made some more tests.
During my investigation I overrided the Invalidate method of the VisualElementRenderer (to check the dispose). I removed it and the error with the DefaultRenderer is gone.
So you right, there is something broken in the runtime with the overriding of this method.

@kvpt hehe yea...

During my investigation I overrided the Invalidate method of the VisualElementRenderer (to check the dispose). I removed it and the error with the DefaultRenderer is gone.

That's basically how I happened upon this behavior. I added an invalidate override inside DefaultRenderer to try and follow the invalidation path and started getting the exception there.

Once I can get a bit more specifics from the XA team on what they think about it I'll get back to you.

I think my PR works for now though. That SkipInvalidate was added a really long time ago and I don't think it's doing that much anymore

is it the PR #9034 related? or another one?

Same Problem +1

Same Problem +1

Any update, in which XF version will get this resolve

Hi there, we now have quite a few pages broken in our app since we upgraded to the latest xamarin forms
Will this be available in the next release?
thanks

The fix has been merged into 4.4 which should be released soon

closed by #9034

@samhouts Fixed for us in the latest X.Forms 4.4.0991537 release.
Thx for that.

@samhouts XF 4.5 have same issue

System.NotSupportedException: 'Unable to activate instance of type Xamarin.Forms.Platform.Android.ImageButtonRenderer from native handle 0xfff4abbc (key_handle 0xa188909).'
XF 4.5.0.530

@haiduong741 Can you provide the full exception stacktrace.
It can be the same cause as before (Invalidate method) or another issue with the same result.

@haiduong741 Can you provide the full exception stacktrace.
It can be the same cause as before (Invalidate method) or another issue with the same result.

9880

Same issue.

So, is this fixed? I am receiving the LabelRenderer exception.

Stack trace:

  at Java.Interop.TypeManager.CreateInstance (System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer, System.Type targetType) [0x00182] in <f0c385c91ba54fe48d1d4683597ba21a>:0 
  at Java.Lang.Object.GetObject (System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer, System.Type type) [0x000c1] in <f0c385c91ba54fe48d1d4683597ba21a>:0 
  at Java.Lang.Object._GetObject[T] (System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer) [0x00017] in <f0c385c91ba54fe48d1d4683597ba21a>:0 
  at Java.Lang.Object.GetObject[T] (System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer) [0x00000] in <f0c385c91ba54fe48d1d4683597ba21a>:0 
  at Java.Lang.Object.GetObject[T] (System.IntPtr jnienv, System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer) [0x00006] in <f0c385c91ba54fe48d1d4683597ba21a>:0 
  at Android.Views.View.n_Invalidate (System.IntPtr jnienv, System.IntPtr native__this) [0x00000] in <f0c385c91ba54fe48d1d4683597ba21a>:0 
  at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.47(intptr,intptr)

My Xamarin.Forms version: 4.4.0.991210-pre2.

My Xamarin.Forms version: 4.4.0.991210-pre2.

No, you need to update to 4.4.0.991537

My Xamarin.Forms version: 4.4.0.991210-pre2.

No, you need to update to 4.4.0.991537

@school-today 's workaround did the trick, although with some artefacts. Upgraded to 4.6.0 itself, and it got fixed. I have a question though: Why are XF upgrades inconsistent with respect to each platform. The reason we never upgraded to 4.6.0 is because, it broke certain things in iOS and certain in Android. Mind you they weren't similar issues. Why? Like, in iOS images don't load and there's simply a blank white screen instead and in Android, SSL fails.

Was this page helpful?
0 / 5 - 0 ratings