wcoder / Xamarin.BlurView

Dynamic iOS-like blur of underlying Views for Android.
https://github.com/Dimezis/BlurView
Apache License 2.0
27 stars 2 forks source link

NullReferenceException / Java.Lang.NullPointerException #8

Closed deeprobin closed 3 years ago

deeprobin commented 3 years ago

I get the following java interop exception:

Java.Lang.NullPointerException
  Message=Attempt to invoke virtual method 'void android.view.ViewGroup.getLocationOnScreen(int[])' on a null object reference

Renderer source code:

public FormsBlurViewRenderer(Context context) : base(context)
        {
        }

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

            if (Control != null)
            {
                return;
            }
            var context = Context;
            var activity = context as Activity;

            var rootView = (ViewGroup)activity?.Window?.DecorView?.FindViewById(Android.Resource.Id.Content);

            var blurView = new BlurView(context);

            blurView.SetupWith(rootView)
                .SetBlurRadius(3F)
                .SetOverlayColor(Resource.Color.foreground_material_light)
                .SetBlurAlgorithm(new RenderScriptBlur(context))
                .SetBlurRadius(10f);

            SetNativeControl(blurView);
        }

Implementation source code:

<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MyNamespace.MyClass" xmlns:local="clr-namespace:MyNamespace;assembly=MySolution">
    <ContentView.GestureRecognizers>
        <TapGestureRecognizer Tapped="TapGestureRecognizer_OnTapped" />
    </ContentView.GestureRecognizers>
    <ContentView.Content>
        <local:FormsBlurView>
            <RelativeLayout HeightRequest="150" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" BackgroundColor="Red">
                <Image x:Name="PreviewImage" Aspect="AspectFill" HorizontalOptions="CenterAndExpand" />
                <BoxView x:Name="FavoriteView" HeightRequest="150" HorizontalOptions="CenterAndExpand" />

                <Label x:Name="TitleLabel" HeightRequest="150" TextColor="#FFFFFF" FontSize="24" HorizontalTextAlignment="End" VerticalTextAlignment="End" HorizontalOptions="FillAndExpand" VerticalOptions="EndAndExpand" Margin="10, 0" />
            </RelativeLayout>
        </local:FormsBlurView>
    </ContentView.Content>
</ContentView>
wcoder commented 3 years ago

Hi, sorry for the delay.

This library currently does not provide support for Xamarin.Forms out of the box, but you can do it via custom controls or effects.

Also, highly recommended to use Sharpnado.MaterialFrame to make views blurring.