2
votes

I am using my own customrenderer for Webviews it has always worked but since I upgraded Xamarin Forms nuget package to version 2.5 it crashes because it seems like the member method OnElementChanged is called with null Native control and null thisActivity. any idea how to fix it?

this is the error I get:

Unhandled Exception:

System.ArgumentNullException: Value cannot be null.
Parameter name: thisActivity
2
Are you sure that the OnElementChanged is not called again a short moment later with those values set? Then you might get away with checking on null and do nothing when it its null. Like its done with their own renderers? github.com/xamarin/Xamarin.Forms/blob/master/… - J. Vergeer
@J.Vergeer No it doesn't it just calls OnElementChanged only once and in that call I get the exception once I call base.OnElementChanged() - fire in the hole
Also clean your solution, delte obj and bin folder of the project, restart vs/xamarin studio, add the ctor that @Shaegorath had given - Csharpest
@Csharpest yeah cleaning and all those stuff wasn't helpfull, btw I'm Shaegorath :D - fire in the hole
Oh yeah hahah i didn't notice that :D So does your self-provided answer fix your problem? you marked it that way - Csharpest

2 Answers

0
votes

I fixed this crash by adding a constructor which takes a Androd.Content.Context and passes it down to the base class in my custom PageRenderer which contains my custom WebViewRenderer. Adding the new constructor to the webview itself wasn't helpful.

public class BaseControllerRenderer : Xamarin.Forms.Platform.Android.PageRenderer
{
    public BaseControllerRenderer(Context context) : base(context)
    {

    }
...
0
votes

From the Xamarin.Forms 2.5 release notes:

WebViewRenderer: This constructor is obsolete as of version 2.5. Please use WebViewRenderer(Context) instead.

Example constructor for the WebView custom renderer and OnElementChanged:

public CustomWebViewRenderer(Context context) : base(context)
{
  Activity activity = (Activity)context;
}

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

  if (Control != null)
  {
    Android.Webkit.WebView webView = Control;
  }
}

See also WebViewRenderer

Edit: Please note that the constructor change applies to any Android custom renderers as described in the Xamarin.Forms 2.5 release notes:

Supporting Forms Embedding required a significant number of deprecations in the Android backend. Please update any custom renderers.