Baseflow / XF-Material-Library

A Xamarin Forms library for implementing Material Design
https://baseflow.com
MIT License
647 stars 161 forks source link

Cannot access a disposed object. Object name: 'AndroidX.AppCompat.Widget.AppCompatButton'. #336

Open shubhambhosale opened 4 years ago

shubhambhosale commented 4 years ago

🐛 Bug Report

I am getting below Exception in Android on emulator as well as on the device, at when we stop and any task before completing, still not sure how I am able to reproduce this issue. But, it is sure that there is something is missing in the plugin. Is this happening because of AndroidX or is there i am missing something. Can you please look into this. Thank you.

Platform: Android Xamarin.Forms: Version - 4.7.0.968 XF.Material: Version - 1.6.5

Exception

{System.ObjectDisposedException: Cannot access a disposed object. Object name: 'AndroidX.AppCompat.Widget.AppCompatButton'. at Java.Interop.JniPeerMembers.AssertSelf (Java.Interop.IJavaPeerable self) [0x00029] in <26521a5118b44c858c385715922b9d5d>:0 at Java.Interop.JniPeerMembers+JniInstanceMethods.InvokeVirtualVoidMethod (System.String encodedMember, Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue* parameters) [0x00000] in <26521a5118b44c858c385715922b9d5d>:0 at Android.Views.View.set_StateListAnimator (Android.Animation.StateListAnimator value) [0x00031] in <d3b924763d4a465c85b26f6e8edc8a53>:0 at XF.Material.Droid.Renderers.MaterialDrawableHelper.UpdateElevation () [0x00008] in Z:\Documents\OpenSource\XF-Material-Library\XF.Material\Platforms\Android\Renderers\MaterialDrawableHelper.cs:286 at XF.Material.Droid.Renderers.MaterialDrawableHelper.BindableButton_PropertyChanged (System.Object sender, System.ComponentModel.PropertyChangedEventArgs e) [0x00029] in Z:\Documents\OpenSource\XF-Material-Library\XF.Material\Platforms\Android\Renderers\MaterialDrawableHelper.cs:110 at Xamarin.Forms.BindableObject.OnPropertyChanged (System.String propertyName) [0x00000] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:229 at Xamarin.Forms.Element.OnPropertyChanged (System.String propertyName) [0x00000] in D:\a\1\s\Xamarin.Forms.Core\Element.cs:353 at XF.Material.Forms.UI.MaterialButton.OnPropertyChanged (System.String propertyName) [0x0001a] in Z:\Documents\OpenSource\XF-Material-Library\XF.Material\UI\MaterialButton.cs:108 at 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) [0x00114] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:461 at Xamarin.Forms.BindableObject.SetValueCore (Xamarin.Forms.BindableProperty property, System.Object value, Xamarin.Forms.Internals.SetValueFlags attributes, Xamarin.Forms.BindableObject+SetValuePrivateFlags privateAttributes) [0x00173] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:397 at Xamarin.Forms.BindingExpression.ApplyCore (System.Object sourceObject, Xamarin.Forms.BindableObject target, Xamarin.Forms.BindableProperty property, System.Boolean fromTarget) [0x00214] in D:\a\1\s\Xamarin.Forms.Core\BindingExpression.cs:155 at Xamarin.Forms.BindingExpression.Apply (System.Boolean fromTarget) [0x0003e] in D:\a\1\s\Xamarin.Forms.Core\BindingExpression.cs:53 at Xamarin.Forms.BindingExpression+BindingExpressionPart.<PropertyChanged>b__49_0 () [0x00000] in D:\a\1\s\Xamarin.Forms.Core\BindingExpression.cs:738 at Xamarin.Forms.BindingExpression+BindingExpressionPart.PropertyChanged (System.Object sender, System.ComponentModel.PropertyChangedEventArgs args) [0x000be] in D:\a\1\s\Xamarin.Forms.Core\BindingExpression.cs:749 at Xamarin.Forms.BindingExpression+WeakPropertyChangedProxy.OnPropertyChanged (System.Object sender, System.ComponentModel.PropertyChangedEventArgs e) [0x00012] in D:\a\1\s\Xamarin.Forms.Core\BindingExpression.cs:642

softlion commented 4 years ago

This happens because you closed the page - disposing all native views - then you trigger a property change in the control.

It may happen because your Command bound to this control has the second parameter set (canExecute). You may be triggering CanExecuteChange after the page containing the control is closed. Changing the state of canExecute will change the IsEnabled property on this control, which leads to the crash you are seing, as this control is not proofsafe against that case.

See https://docs.microsoft.com/en-us/dotnet/api/xamarin.forms.command.-ctor?view=xamarin-forms#Xamarin_Forms_Command__ctor_System_Action_System_Object__System_Func_System_Object_System_Boolean__

Feel free to clone, modify the code and PR !

felipechavez commented 4 years ago

I am facing the same problem, I just put a button on one page, then on another page I change current theme and app dies. It's totally weird. There is any workaround about this?

ramonB1996 commented 4 years ago

Possible duplicate of #365 I had this same error message and am pretty sure it is the same problem.