System.ObjectDisposedException: Cannot access a disposed object when using ScrollView

samgaksamgak NZMember ✭✭
edited May 2017 in Xamarin.Forms

I have a Xamarin.Forms app that includes a ContentPage with a ScrollView, where the ScrollView is initially hidden, but in response to a UI event I show it by setting IsVisible to true and calling ScrollToAsync on the ScrollView. After recently upgrading Xamarin.Forms, this causes this exception:

Xamarin caused by: android.runtime.JavaProxyThrowable: System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'Xamarin.Forms.Platform.Android.ScrollViewRenderer'.
Java.Interop.JniPeerMembers.AssertSelf(IJavaPeerable self)<3a653d21f22f4786bf9d5acf69f4a22e>:0
Java.Interop.JniPeerMembers.JniInstanceMethods.InvokeVirtualBooleanMethod(string encodedMember, IJavaPeerable self, JniArgumentValue* parameters)<3a653d21f22f4786bf9d5acf69f4a22e>:0
Java.Lang.IRunnableInvoker.n_Run(IntPtr jnienv, IntPtr native__this)<cba5498252974515814d3da6e57d094c>:0
at (wrapper dynamic-method) System.Object:ba3ec2cc-2626-44c0-ab76-80f16217bdbf (intptr,intptr) Method)
java.lang.reflect.Method.invoke(Native Method)$

I managed to find a workaround, which is to call ScrollToAsync like this:

public async Task DelayActionAsync(int delay, Action action) 
    await Task.Delay(delay);


and replace my ScrollToAsync call with this:

 DelayActionAsync(100, () => {myScrollView.ScrollToAsync(positionX, positionY, false);} );

and this gets rid of the crash (sorry for any errors, this is typed from memory).

My question is, why do I have to do this? Is this just a bug in Xamarin.Forms that I am working around, or is this the recommended and correct way, and I was just lucky it wasn't crashing previously? If the latter, can anyone point me to any documentation about why this is so?

The crash only occurred on certain Android devices, and only in release builds. That doesn't mean it's a Xamarin bug of course because it could be a timing bug in my code that only gets triggered on certain devices. I just want to know how to code defensively to avoid these errors.


Sign In or Register to comment.