System.ObjectDisposedException: Can't access disposed object. Object name: 'Android.Graphics.Bitmap'

wchiouwchiou USMember

Here is the stack trace

System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'Android.Graphics.Bitmap'.
at Java.Interop.JniPeerMembers.AssertSelf (Java.Interop.IJavaPeerable self) [0x00030] in /Users/builder/data/lanes/4468/b16fb820/source/Java.Interop/src/Java.Interop/Java.Interop/JniPeerMembers.cs:153
at Java.Interop.JniPeerMembers+JniInstanceMethods.InvokeAbstractVoidMethod (System.String encodedMember, Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue* parameters) [0x00002] in /Users/builder/data/lanes/4468/b16fb820/source/Java.Interop/src/Java.Interop/Java.Interop/JniPeerMembers.JniInstanceMethods_Invoke.cs:11
at Android.Graphics.Bitmap.Recycle () [0x00000] in /Users/builder/data/lanes/4468/b16fb820/source/monodroid/src/Mono.Android/platforms/android-25/src/generated/Android.Graphics.Bitmap.cs:975
at Xamarin.Forms.Platform.Android.ButtonDrawable.Reset () [0x00008] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\Renderers\ButtonDrawable.cs:55
at Xamarin.Forms.Platform.Android.ButtonRenderer.OnElementChanged (Xamarin.Forms.Platform.Android.ElementChangedEventArgs1[TElement] e) [0x00067] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\Renderers\ButtonRenderer.cs:110 at Xamarin.Forms.Platform.Android.VisualElementRenderer1[TElement].SetElement (TElement element) [0x000f4] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\VisualElementRenderer.cs:196
at Xamarin.Forms.Platform.Android.VisualElementRenderer1[TElement].Xamarin.Forms.Platform.Android.IVisualElementRenderer.SetElement (Xamarin.Forms.VisualElement element) [0x00027] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\VisualElementRenderer.cs:137 at Xamarin.Forms.Platform.Android.VisualElementPackager.AddChild (Xamarin.Forms.VisualElement view, Xamarin.Forms.Platform.Android.IVisualElementRenderer oldRenderer, Xamarin.Forms.Platform.Android.RendererPool pool, System.Boolean sameChildren) [0x0003a] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\VisualElementPackager.cs:91 at Xamarin.Forms.Platform.Android.VisualElementPackager.SetElement (Xamarin.Forms.VisualElement oldElement, Xamarin.Forms.VisualElement newElement) [0x00104] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\VisualElementPackager.cs:217 at Xamarin.Forms.Platform.Android.VisualElementPackager.<.ctor>b__6_0 (System.Object sender, Xamarin.Forms.Platform.Android.VisualElementChangedEventArgs args) [0x00000] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\VisualElementPackager.cs:29 at Xamarin.Forms.Platform.Android.VisualElementRenderer1[TElement].OnElementChanged (Xamarin.Forms.Platform.Android.ElementChangedEventArgs1[TElement] e) [0x00031] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\VisualElementRenderer.cs:292 at Xamarin.Forms.Platform.Android.VisualElementRenderer1[TElement].SetElement (TElement element) [0x000f4] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\VisualElementRenderer.cs:196
at Xamarin.Forms.Platform.Android.VisualElementRenderer`1[TElement].Xamarin.Forms.Platform.Android.IVisualElementRenderer.SetElement (Xamarin.Forms.VisualElement element) [0x00027] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\VisualElementRenderer.cs:137
at Xamarin.Forms.Platform.Android.ViewCellRenderer+ViewCellContainer.Update (Xamarin.Forms.ViewCell cell) [0x00093] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\Cells\ViewCellRenderer.cs:102
at Xamarin.Forms.Platform.Android.ViewCellRenderer.GetCellCore (Xamarin.Forms.Cell item, Android.Views.View convertView, Android.Views.ViewGroup parent, Android.Content.Context context) [0x00011] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\Cells\ViewCellRenderer.cs:18
at Xamarin.Forms.Platform.Android.CellRenderer.GetCell (Xamarin.Forms.Cell item, Android.Views.View convertView, Android.Views.ViewGroup parent, Android.Content.Context context) [0x00063] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\Cells\CellRenderer.cs:51
at Xamarin.Forms.Platform.Android.CellFactory.GetCell (Xamarin.Forms.Cell item, Android.Views.View convertView, Android.Views.ViewGroup parent, Android.Content.Context context, Xamarin.Forms.View view) [0x00023] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\Cells\CellFactory.cs:20
at Xamarin.Forms.Platform.Android.ListViewAdapter.GetView (System.Int32 position, Android.Views.View convertView, Android.Views.ViewGroup parent) [0x001d7] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\Renderers\ListViewAdapter.cs:258
at Android.Widget.BaseAdapter.n_GetView_ILandroid_view_View_Landroid_view_ViewGroup_ (System.IntPtr jnienv, System.IntPtr native__this, System.Int32 position, System.IntPtr native_convertView, System.IntPtr native_parent) [0x0001a] in /Users/builder/data/lanes/4468/b16fb820/source/monodroid/src/Mono.Android/platforms/android-25/src/generated/Android.Widget.BaseAdapter.cs:443
at at (wrapper dynamic-method) System.Object:547b43a6-8c3b-4ca3-8c70-6d5cccce980b (intptr,intptr,int,intptr,intptr)

Here is my xaml

<?xml version="1.0" encoding="UTF-8"?>





<?xml version="1.0" encoding="UTF-8"?>

        <Button StyleId="load" Text="Load" Font="Medium" 
            MinimumWidthRequest="60" MinimumHeightRequest="25"
            BackgroundColor="{l:GlobalResource DefaultButtonBackground}" 
            TextColor="{l:GlobalResource DefaultButtonText}" 
            Clicked="LoadClicked" CommandParameter="{Binding Location}" />
        <Label TextColor="{l:GlobalResource LightestText}"
            HorizontalOptions="FillAndExpand" VerticalOptions="Center" 
            Text="{Binding Location.DisplayName}"/>
        <Button Clicked="LocationSelected" CommandParameter="{Binding Location}"
            BackgroundColor="{l:GlobalResource TransparentBackground}" 
            MinimumWidthRequest="20" WidthRequest="20" 
            HorizontalOptions="Center" VerticalOptions="Center" 
            Image="disclosure.png" IsVisible="{Binding Location.Show}"/>

</StackLayout>

<?xml version="1.0" encoding="UTF-8"?>

<ContentPage.Content>











</ContentPage.Content>

    public CurrentLoad()
    {

        InitializeComponent();
        Device.BeginInvokeOnMainThread( () => {
            VehiclePickupList.ItemTemplate = new DataTemplate(typeof(VehiclePickupCell));
            VehiclePickupList.GroupHeaderTemplate = new DataTemplate(typeof(VehiclePickupCellHeader));
            VehiclePickupList.ItemsSource = ViewModel.VehiclesGroupedByPickup;

            VehicleDeliveryList.ItemTemplate = new DataTemplate(typeof(VehicleDeliveryCell));
            VehicleDeliveryList.GroupHeaderTemplate = new DataTemplate(typeof(VehicleDeliveryCellHeader));
            VehicleDeliveryList.IsVisible = false;
            VehicleDeliveryList.ItemsSource = ViewModel.VehiclesGroupedByDropoff;
        });

        ......
    }

can someone shed some light why i get the exception on the disposed object please?

Best Answer

Answers

  • PaulMikeDevPaulMikeDev Member

    I had also same Issue with my Xamarin Forms Project(Android devices) .

    Issue:
    System.ObjectDisposedException: Can't access disposed object. Object name: 'Android.Graphics.Bitmap'

    This below steps Worked for me

    1. Create a Text file with any name in your xyz.Android project .
    2. Write this code inside that text file
      MONO_GC_PARAMS=bridge-implementation=new
    3. Right click on this text file, got to properties.
    4. Find BuildAction option, Click dropdown and select Android Environment.
    5. Save,clean project,rebuild and run your project.
  • taterjuicetaterjuice Member ✭✭
    edited August 2018

    I had this issue as well, and ultimately found that it was actually an issue with the ViewCell Disposal, but NOT because of manipulating the ViewCell, it seems to be caused by a race condition when setting the ListView.ItemsSource or ListView.ItemTemplate during a CollectionChanged event, and it only occurs on Android.

    In XF 3.x+, I can intermittently reproduce this by repeatedly setting the ListView.ItemsSource in codebehind while the ListView is still processing CollectionChanged events from the previously bound ItemsSource.

    This is a code-sample to REPRODUCE the issue, it is not production code.

    lv.ItemsSource = dc.Items;
    await Task.Delay(100);
    for(int i = 0; i < 100; i++)
    {
        var itemTemplateCopy = lv.ItemTemplate;
        lv.ItemTemplate = null;
        lv.ItemsSource = null;
        await Task.Delay(100);
        dc.Items.Add(String.Format("Item #{0}", i));
        lv.ItemTemplate = itemTemplateCopy;
        lv.ItemsSource = dc.Items;
    }
    
  • shashidharKSshashidharKS USMember ✭✭

    I was also having this problem,

    @ PaulMikeDev What exactly it does?

    @ taterjuice Do you got any solution for it?

  • felsfels Member ✭✭

    @PaulMikeDev said:
    I had also same Issue with my Xamarin Forms Project(Android devices) .

    Issue:
    System.ObjectDisposedException: Can't access disposed object. Object name: 'Android.Graphics.Bitmap'

    This below steps Worked for me

    1. Create a Text file with any name in your xyz.Android project .
    2. Write this code inside that text file
      MONO_GC_PARAMS=bridge-implementation=new
    3. Right click on this text file, got to properties.
    4. Find BuildAction option, Click dropdown and select Android Environment.
    5. Save,clean project,rebuild and run your project.

    So my problem was getting this exception when doing async tasks for objects not on screen or setting my listviews itemssource while doing those tasks.
    And although I don't know what your solution's doing, it worked for me. Thank you!

Sign In or Register to comment.