Xamarin.forms: [Bug] "Unable to activate instance of type Xamarin.Forms.Platform.Android.FastRenderers.LabelRenderer from native handle"

Created on 9 Jan 2020  ·  39Comments  ·  Source: xamarin/Xamarin.Forms

Description

Since updating our Xamarin Forms solution from 4.2.0.848062 to 4.4.0.991265 we are seeing errors like the above start appearing at various places within the application, only on the Android version of the app of course.

Sorry for the lack of specific repo steps but I am adding this more as a way to see if this is unique to my apps or if there is a general issue with the newest Forms release on Android.

Basic Information

  • Version with issue: 4.4.0.991265
  • Last known good version: 4.2.0.848062
  • IDE:
  • Platform Target Frameworks:

    • Android: 9.0 (Pie)

  • Android Support Library Version: 28.0.0.3
needs-info ❓ unverified bug

Most helpful comment

try this

        public class CustomImageButtonRenderer : ImageButtonRenderer
        {
            public CustomImageButtonRenderer(Context context) : base(context)
            { 
            }

            [Obsolete]
            public CustomImageButtonRenderer(IntPtr handle, global::Android.Runtime.JniHandleOwnership transfer)
                : base(Forms.Context)
            { 

            }
        }

All 39 comments

@GZidar Can you give us some more details on your application? What sort of controls are you seeing these errors from? Are you using ListViews in your application? CollectionViews? What sort of navigation?

This is most likely fixed by this PR

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

If you're still having this issue with the next release of forms then can you answer @hartez 's questions and we'll reopen

@PureWeen I added the nightly feed to my project and I can confirm that the LabelRenderer problem has gone away, BUT now a similar error still appears except now it is complaining about ImageButtonRenderer instead so perhaps that has the same issue.

To answer @hartez questions the particular page with the crash does not have a ListView or CollectionView but does have a StackLayout with a BindableLayout template from a collection. The datatemplate includes labels and image buttons and I can confirm that this error is only shown if there are items in the list (ie an empty list allows the page to load without errors).

Would it be possible to reopen this issue?

Further to the above, I have done some more poking around and if I change the control from StackLayout with BindableLayout inside a ScrollView to use CollectionView the page now loads without error even with data in the list. Hopefully that information will help narrow the search for whatever the problem happens to be.

Code snippet that fails:

                <ScrollView Grid.Row="3"
                            Grid.Column="0"
                            HorizontalOptions="FillAndExpand"
                            VerticalOptions="FillAndExpand"
                            >

                    <StackLayout HorizontalOptions="FillAndExpand"
                                 VerticalOptions="FillAndExpand"
                                 Orientation="Vertical"
                                 Margin="0,0,0,0"
                                 Padding="0,6,0,0"
                                 >
                        <StackLayout BindableLayout.ItemsSource="{Binding Jobs}">
                            <BindableLayout.ItemTemplate>
                                <DataTemplate>

This code snippet allows the page to load:

                <StackLayout HorizontalOptions="FillAndExpand"
                            VerticalOptions="FillAndExpand"
                            Grid.Row="3"
                            Grid.Column="0"
                            Orientation="Vertical"
                            Margin="0,0,0,0"
                            Padding="0,6,0,0"
                            >
                    <CollectionView ItemsSource="{Binding Jobs}">
                        <CollectionView.ItemTemplate>
                            <DataTemplate>

the data template is unchanged between the two pages

@GZidar do you have the stack trace for ImageButton failure?

I have a pretty good idea of why that one is probably failing and it's most likely the same reason the LabelRenderer was failing

But the fix there is a bit trickier

stack trace from AppCenter is here:

Java.Lang.Error: Exception of type 'Java.Lang.Error' was thrown.
java.lang.Error: Java callstack:
crc643f46942d9dd1fff9.ImageButtonRenderer.n_invalidate(Native Method)
crc643f46942d9dd1fff9.ImageButtonRenderer.invalidate ImageButtonRenderer.java:53
android.view.ViewRootImpl$InvalidateOnAnimationRunnable.run ViewRootImpl.java:8643
android.view.Choreographer$CallbackRecord.run Choreographer.java:949
android.view.Choreographer.doCallbacks Choreographer.java:761
android.view.Choreographer.doFrame Choreographer.java:693
android.view.Choreographer$FrameDisplayEventReceiver.run Choreographer.java:935
android.os.Handler.handleCallback Handler.java:873
android.os.Handler.dispatchMessage Handler.java:99
android.os.Looper.loop Looper.java:214
android.app.ActivityThread.main ActivityThread.java:7050
java.lang.reflect.Method.invoke(Native Method)
com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run RuntimeInit.java:493
com.android.internal.os.ZygoteInit.main ZygoteInit.java:965

Also this is included in the crash report:

TypeManager.CreateProxy (System.Type type, System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer)
TypeManager.CreateInstance (System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer, System.Type targetType)
TypeManager.CreateInstance (System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer, System.Type targetType)
Object.GetObject (System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer, System.Type type)
Object._GetObject[T] (System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer)
Object.GetObject[T] (System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer)
Object.GetObject[T] (System.IntPtr jnienv, System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer)
View.n_Invalidate (System.IntPtr jnienv, System.IntPtr native__this)
(wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.58(intptr,intptr)

Hi @PureWeen and @GZidar. I just ran into this issue with ImageButtonRenderer, but wanted to point out that we've explicitly disabled FastRenderers. Are there any known work-arounds with eg. a custom ImageButton renderer?

System.NotSupportedException
Message=Unable to activate instance of type Xamarin.Forms.Platform.Android.ImageButtonRenderer from native handle 0xbfa68aac (key_handle 0x4a39312).

0xFFFFFFFFFFFFFFFF in System.Diagnostics.Debugger.Mono_UnhandledException_internal  C#
0x1 in System.Diagnostics.Debugger.Mono_UnhandledException at /Users/builder/jenkins/workspace/archive-mono/2019-08/android/release/mcs/class/corlib/System.Diagnostics/Debugger.cs:120,4   C#
0x20 in Android.Runtime.DynamicMethodNameCounter.71 C#
0x182 in Java.Interop.TypeManager.CreateInstance    C#
0xC4 in Java.Lang.Object.GetObject  C#
0x23 in Java.Lang.Object._GetObject<Android.Views.View> C#
0x2 in Java.Lang.Object.GetObject<Android.Views.View>   C#
0x8 in Java.Lang.Object.GetObject<Android.Views.View>   C#
0x3 in Android.Views.View.n_Invalidate  C#
0x11 in Android.Runtime.DynamicMethodNameCounter.71 C#

@jalbertSyncroTech I believe that ImageButton only has fast renderer (as in there is no non-fast renderer available for ImageButton) so disabling fast renderers did solve the previous issue with labels but will not overcome the problem with image buttons... as a workaround in paces where you cannot use collection view - because of the way it consumes the vertical space on the screen for example - you might try using a plain image and then adding tapped gesture event handler to that until the fix for this issue is available.

I have the same issue with ImageButtonRenderer but I've updated from v 4.3 to latest stable version 4.5.0.356 Any updates related to this? I can't change the layout view and I don't have any custom renderers for that element. Text of exception: System.NotSupportedException: 'Unable to activate instance of type Xamarin.Forms.Platform.Android.ImageButtonRenderer from native handle 0xffdefd0c

Also getting this (for ImageButtonRenderer) after upgrading to XF 4.5.0.356 . Didn't happen before upgrading (for completeness, I also updated a few other NuGets at the same time, but none that interact with ImageButtons).

System.NotSupportedException
Message=Unable to activate instance of type Xamarin.Forms.Platform.Android.ImageButtonRenderer from native handle 0x7fcb2a3564 (key_handle 0x4dc4486).

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

I am also having the same issue. Same thing - started after upgrading to 4.5.0.530. The page loads fine the first time that has my renderer. But any subsequent times the app crashes with this error. I am open to suggestions...

@mikeluken upgrading from what?

Here's an open issue for tracking ImageButtonRenderer
https://github.com/xamarin/Xamarin.Forms/issues/10123

If anyone has a repro that would be helpful

I honestly don't remember what version of Xamarin.Forms I was using before upgrading to 4.5.0.530. I want to say it was a few versions back - something like 4.3.xxx.

Actually I am on 4.6.0.616-pre4. Same issue though.

@mikeluken is your imagebutton inside a listview or collectionview?

Yes. It is actually inside a SfListView (syncfusion).

do you have a full stack trace @mikeluken

I'm just curious if it's the same issue as I linked to above

04-15 13:06:40.820 I/MonoDroid( 8461): UNHANDLED EXCEPTION:
04-15 13:06:40.822 I/MonoDroid( 8461): System.NotSupportedException: Unable to activate instance of type Xamarin.Forms.Platform.Android.ImageButtonRenderer from native handle 0x7fe2d1a3d4 (key_handle 0x203f126). ---> System.MissingMethodException: No constructor found for Xamarin.Forms.Platform.Android.ImageButtonRenderer::.ctor(System.IntPtr, Android.Runtime.JniHandleOwnership) ---> Java.Interop.JavaLocationException: Exception of type 'Java.Interop.JavaLocationException' was thrown.
04-15 13:06:40.822 I/MonoDroid( 8461): --- End of inner exception stack trace ---
04-15 13:06:40.822 I/MonoDroid( 8461): at Java.Interop.TypeManager.CreateProxy (System.Type type, System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer) [0x00036] in /Users/runner/runners/2.165.0/work/1/s/xamarin-android/src/Mono.Android/Java.Interop/TypeManager.cs:312
04-15 13:06:40.822 I/MonoDroid( 8461): at Java.Interop.TypeManager.CreateInstance (System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer, System.Type targetType) [0x00116] in /Users/runner/runners/2.165.0/work/1/s/xamarin-android/src/Mono.Android/Java.Interop/TypeManager.cs:290
04-15 13:06:40.822 I/MonoDroid( 8461): --- End of inner exception stack trace ---
04-15 13:06:40.822 I/MonoDroid( 8461): at Java.Interop.TypeManager.CreateInstance (System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer, System.Type targetType) [0x00158] in /Users/runner/runners/2.165.0/work/1/s/xamarin-android/src/Mono.Android/Java.Interop/TypeManager.cs:297
04-15 13:06:40.822 I/MonoDroid( 8461): at Java.Lang.Object.GetObject (System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer, System.Type type) [0x000c1] in /Users/runner/runners/2.165.0/work/1/s/xamarin-android/src/Mono.Android/Java.Lang/Object.cs:467
04-15 13:06:40.822 I/MonoDroid( 8461): at Java.Lang.Object._GetObject[T] (System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer) [0x00017] in /Users/runner/runners/2.165.0/work/1/s/xamarin-android/src/Mono.Android/Java.Lang/Object.cs:438
04-15 13:06:40.822 I/MonoDroid( 8461): at Java.Lang.Object.GetObject[T] (System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer) [0x00000] in /Users/runner/runners/2.165.0/work/1/s/xamarin-android/src/Mono.Android/Java.Lang/Object.cs:430
04-15 13:06:40.822 I/MonoDroid( 8461): at Java.Lang.Object.GetObject[T] (System.IntPtr jnienv, System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer) [0x00006] in /Users/runner/runners/2.165.0/work/1/s/xamarin-android/src/Mono.Android/Java.Lang/Object.cs:424
04-15 13:06:40.822 I/MonoDroid( 8461): at Android.Views.View.n_Invalidate (System.IntPtr jnienv, System.IntPtr native__this) [0x00000] in /Users/runner/runners/2.165.0/work/1/s/xamarin-android/src/Mono.Android/obj/Release/android-29/mcw/Android.Views.View.cs:16083

Yea that's probably the same issue

So is there a workaround? Or do we need to just wait for a fix included in the next Xamarin.Forms? I only ask because we have testers standing by and I need to let them know if they should be expecting a fixed build today or not.

@mikeluken switching to a button with an image or disabling recycling on SfListView (if it supports it) should resolve the issue

Setting it to an ImageButton (from an image) also creates the same issue. I don't see any option to disable recycling.

yea Image and ImageButton share the same breaking concept

So I was saying use a Button with the Image set to the image you want to use

The problem with using a regular button is that I can't set the Aspect. Image and ImageButton both have AspectFill which I need to fill the area. Is the fix for Image not going to happen soon I take it?

Not this soon

I only ask because we have testers standing by and I need to let them know if they should be expecting a fixed build today or not.

so just trying to help with a today workaround

If you switch back to legacy renderers that might get around the issue

Forms.SetFlags("UseLegacyRenderers");

I already tried that. I still get the same error using Legacy Renderers. Do you know how soon it could potentially be available? This is having a pretty large impact on us.

IT's hard to say exactly

I still don't have a repro so I'm still just guessing. I'm 80 percent sure I'm right but there's a chance I'll make the change with no ability to test it and it'll still be broken

If you have a basic repro of it breaking then I can probably fix it quicker and more reliably

@mikeluken another workaround you could try is to implement a custom renderer for ImageButton Renderer and then supply the constructor it indicates is missing

` System.MissingMethodException: No constructor found for Xamarin.Forms.Platform.Android.ImageButtonRenderer::.ctor(System.IntPtr, Android.Runtime.JniHandleOwnership)

I actually had already tried that. Perhaps I am doing it wrong, but this doesn't seem to make a difference at all.

In shared project:

namespace MyApp.UI.Visuals
{
    public class CustomImageButtonVisual : IVisual
    {
    }
}

In Android project:

[assembly: ExportRenderer(typeof(ImageButton), typeof(MyApp.Droid.Renderers.CustomImageButtonRenderer), new[] { typeof(CustomImageButtonVisual) })]
namespace MyApp.Droid.Renderers
{
    public class CustomImageButtonRenderer : ImageRenderer
    {
        public CustomImageButtonRenderer(Context context) : base(context)
        { }

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

And then on my image:

<Image 
   Source="{Binding CameraSnapshot}" 
   WidthRequest="300" 
   HeightRequest="200"
   Aspect="AspectFill" 
   Visual="CustomImageRenderer"
   AbsoluteLayout.LayoutBounds="1,1,1,1" 
   AbsoluteLayout.LayoutFlags="All" />

I still get the same error:

System.NotSupportedException: 'Unable to activate instance of type Xamarin.Forms.Platform.Android.ImageButtonRenderer from native handle 0x7fe2d1b234 (key_handle 0x6a3c6fb).'

if you don't use a visual does it work? Just do

[assembly: ExportRenderer(typeof(ImageButton), typeof(MyApp.Droid.Renderers.CustomImageButtonRenderer))]
namespace MyApp.Droid.Renderers
{
    public class CustomImageButtonRenderer : ImageButtonRenderer
    {
        public CustomImageButtonRenderer(Context context) : base(context)
        { }

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

then use debugger to make sure the constructor is getting called just to prove your renderer is wiring up correctly

How does it know to use CustomImageButtonRenderer if I don't use a visual?

this

[assembly: ExportRenderer(typeof(ImageButton), typeof(MyApp.Droid.Renderers.CustomImageButtonRenderer))]

Tells it to use your customer renderer for all ImageButtons as the default renderer

That gives me a compiler error:

There is no argument that corresponds to the required format parameter 'context' of 'ImageButtonRenderer.ImageButtonRenderer(Context)'

try this

        public class CustomImageButtonRenderer : ImageButtonRenderer
        {
            public CustomImageButtonRenderer(Context context) : base(context)
            { 
            }

            [Obsolete]
            public CustomImageButtonRenderer(IntPtr handle, global::Android.Runtime.JniHandleOwnership transfer)
                : base(Forms.Context)
            { 

            }
        }

Thanks! That did compile and did fix the error that I was having. I appreciate the help for sure! :)

I tested a project with the same error, Xamarin 5.0.0.1539-pre2 and it still persists

System.ObjectDisposedException: Cannot access a dropped object

Using this line Forms.SetFlags ("UseLegacyRenderers") in the project the exception have the ObjectName:
Xamarin.Forms.Platform.Android.AppCompat.FrameRenderer

Without flag line the ObjectName:
Xamarin.Forms.Platform.Android.FastRenderers.FrameRenderer

@TsplayerT that's a different class causing that exception. Can you log a new issue with a full stack trace and repro?

Here it is:

| Main thread | Details |
| ------------------- | ------------------- |
| Java.Interop | JniPeerMembers.AssertSelf (Java.Interop.IJavaPeerable self) |
| Java.Interop | JniPeerMembers+JniInstanceMethods.InvokeVirtualVoidMethod (System.String encodedMember, Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue* parameters) |
| Android.Views | View.set_Enabled (System.Boolean value) |
| Xamarin.Forms.Platform.Android.FastRenderers | VisualElementRenderer.UpdateIsEnabled () |
| Xamarin.Forms.Platform.Android.FastRenderers | VisualElementRenderer.OnElementPropertyChanged (System.Object sender, System.ComponentModel.PropertyChangedEventArgs e) (wrapper delegate-invoke .invoke_void_object_PropertyChangedEventArgs(object,System.ComponentModel.PropertyChangedEventArgs) |
| Xamarin.Forms | BindableObject.OnPropertyChanged (System.String propertyName) |
| Xamarin.Forms | Element.OnPropertyChanged (System.String propertyName) |
| Xamarin.Forms | BindableObject.SetValueActual (Xamarin.Forms.BindableProperty property, Xamarin.Forms.BindableObject+BindablePropertyContext context, System.Object value, System.Boolean currentlyApplying, Xamarin.Forms.Internals.SetValueFlags attributes, System.Boolean silent) |
| Xamarin.Forms | BindableObject.SetValueCore (Xamarin.Forms.BindableProperty property, System.Object value, Xamarin.Forms.Internals.SetValueFlags attributes, Xamarin.Forms.BindableObject+SetValuePrivateFlags privateAttributes) |
| Xamarin.Forms | BindingExpression.ApplyCore (System.Object sourceObject, Xamarin.Forms.BindableObject target, Xamarin.Forms.BindableProperty property, System.Boolean fromTarget) |
| Xamarin.Forms | BindingExpression.Apply (System.Boolean fromTarget) |
| Xamarin.Forms | BindingExpression+BindingExpressionPart.b__49_0 () |
| Java.Lang | Thread+RunnableImplementor.Run () |
| Java.Lang | IRunnableInvoker.n_Run (System.IntPtr jnienv, System.IntPtr native__this) (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.24(intptr,intptr) |

The details of the error and other information are attached. report.txt

Was this page helpful?
0 / 5 - 0 ratings

Related issues

AppGrate picture AppGrate  ·  3Comments

deakjahn picture deakjahn  ·  3Comments

joseluisct picture joseluisct  ·  3Comments

simontocknell picture simontocknell  ·  3Comments

suihanhbr picture suihanhbr  ·  3Comments