App Crash - Cannot access a disposed object.

Couper123Couper123 USMember ✭✭
edited September 11 in Xamarin.Forms

Hi there, I am using a webview in my Xamarin forms app, and I've been receiving a very difficult to reproduce and annoying hard crash, the exception is as follows :

09-07 17:04:06.927 I/MonoDroid(18492): UNHANDLED EXCEPTION: 09-07 17:04:06.947 I/MonoDroid(18492): System.ObjectDisposedException: Cannot access a disposed object. 09-07 17:04:06.947 I/MonoDroid(18492): Object name: 'Android.Webkit.WebView'. 09-07 17:04:06.947 I/MonoDroid(18492): at Java.Interop.JniPeerMembers.AssertSelf (Java.Interop.IJavaPeerable self) [0x00029] in <6cd960837cc24c26bab2a0a29b597627>:0 09-07 17:04:06.947 I/MonoDroid(18492): at Java.Interop.JniPeerMembers+JniInstanceMethods.InvokeVirtualVoidMethod (System.String encodedMember, Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue* parameters) [0x00000] in <6cd960837cc24c26bab2a0a29b597627>:0 09-07 17:04:06.947 I/MonoDroid(18492): at Android.Webkit.WebView.LoadUrl (System.String url) [0x00029] in <d278c06ad5684d6882c743a94a93ebc2>:0 09-07 17:04:06.947 I/MonoDroid(18492): at MaxUserApp.Droid.Renderers.HybridWebViewRenderer+<>c__DisplayClass2_0.<InjectJs>b__0 () [0x00012] in C:\MaxMobile\MaxUserApp\MaxUserApp\MaxUserApp.Droid\Renderers\HybridWebViewRenderer.cs:37 09-07 17:04:06.947 I/MonoDroid(18492): at Java.Lang.Thread+RunnableImplementor.Run () [0x00008] in <d278c06ad5684d6882c743a94a93ebc2>:0 09-07 17:04:06.947 I/MonoDroid(18492): at Java.Lang.IRunnableInvoker.n_Run (System.IntPtr jnienv, System.IntPtr native__this) [0x00008] in <d278c06ad5684d6882c743a94a93ebc2>:0 09-07 17:04:06.947 I/MonoDroid(18492): at (wrapper dynamic-method) System.Object:f721972a-5767-4022-991c-74147ff0679a (intptr,intptr) 09-07 17:04:06.967 W/art (18492): JNI RegisterNativeMethods: attempt to register 0 native methods for android.runtime.JavaProxyThrowable

I haven't been able to reproduce this crash on any other Android version other than 5.11 .

I am using Xamarin forms version 2.3.4.247 .

I'm aware that this is a tall ask to solve based off the above scenario and not knowing the situation, but mainly more curious if anyone has heard of this happening before and anything conceptually they adjusted to solve it.

    using MaxUserApp.Controls;
    using MaxUserApp.Droid.Renderers;
    using Xamarin.Forms;

    [assembly: ExportRenderer(typeof(HybridWebView), typeof(HybridWebViewRenderer))]
    [assembly: Dependency(typeof(HybridWebViewRenderer))]
    namespace MaxUserApp.Droid.Renderers
    {
        using System;
        using Android.OS;
        using Android.Views;
        using Android.Webkit;
        using Controls;
        using Xamarin.Forms.Platform.Android;

        public class HybridWebViewRenderer : Xamarin.Forms.Platform.Android.AppCompat.ViewRenderer<HybridWebView, WebView>,
            IJavascriptInjector
        {
            const string JavaScriptFunction = "function invokeCSharpAction(data){jsBridge.invokeAction(data);}";

        #if DEBUG
            public HybridWebViewRenderer()
            {
                if (Build.VERSION.SdkInt >= BuildVersionCodes.Kitkat)
                {
                    WebView.SetWebContentsDebuggingEnabled(true);
                }
            }
        #endif

            public void InjectJs(string script)
            {
                Device.BeginInvokeOnMainThread(() =>
                {
                    var url = $"javascript: {script}";
                    Control?.LoadUrl(url);
                });
            }

            protected override void OnElementChanged(ElementChangedEventArgs<HybridWebView> e)
            {
                base.OnElementChanged(e);

                if (Control == null)
                {
                    var webView = new WebView(Forms.Context);
                    // Some issue in KitKat (4.4) Android causing the webview to freeze/show a white screen.
                    // https://stackoverflow.com/questions/20675554/webview-rendering-issue-in-android-kitkat
                    if (Build.VERSION.SdkInt == BuildVersionCodes.Kitkat)
                    {
                        webView.SetLayerType(LayerType.Software, null);
                    }
                    webView.Settings.JavaScriptEnabled = true;
                    SetNativeControl(webView);
                }
                if (e.OldElement != null)
                {
                    Control?.RemoveJavascriptInterface("jsBridge");
                    var hybridWebView = e.OldElement;
                    hybridWebView.Cleanup();
                }
                if (e.NewElement != null)
                {
                    Device.BeginInvokeOnMainThread(() =>
                    {
                        Control?.AddJavascriptInterface(new JsBridge(this), "jsBridge");
                        Control?.LoadUrl($"file:///android_asset/Content/{Element.Uri}");
                        InjectJs(JavaScriptFunction);
                        e.NewElement.JavascriptInjector = this;
                    });
                }
            }

            protected override WebView CreateNativeControl()
            {
                throw new NotImplementedException();
            }
        }
    }

Is the source for my webview renderer.

Cheers.

Answers

Sign In or Register to comment.