question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Maui Android Crash CarouselView IndicatorView on navigating second time

See original GitHub issue

Description

Maui Android Crash when there is CarouselView with IndicatorView and navigating second time to the page .

`

<CarouselView ItemsSource="{Binding Item.ProductFiles}" IndicatorView="myIndicator">
        <CarouselView.ItemTemplate>
            <DataTemplate x:DataType="model:ProductFileV2">                    
                <StackLayout>
                    <Frame HasShadow="True"
                   BorderColor="DarkGray"
                   CornerRadius="5"
                   Margin="20"
                   HeightRequest="300"
                   HorizontalOptions="Center"
                   VerticalOptions="CenterAndExpand">
                        <StackLayout>
                            <local:PinchToZoomContainer>
                                <Image Source="{Binding ThumbnailHostPathPublic}"
                               Aspect="AspectFill"
                               HeightRequest="150"
                               WidthRequest="150"
                               HorizontalOptions="Center" />
                            </local:PinchToZoomContainer>
                        </StackLayout>
                    </Frame>
                </StackLayout>                   
                </DataTemplate>
        </CarouselView.ItemTemplate>
    </CarouselView>
    <IndicatorView x:Name="myIndicator"
               IndicatorColor="LightGray"
               SelectedIndicatorColor="DarkGray"
               HorizontalOptions="Center" />

public ObservableCollection<ProductFile> ProductFileList { get; set; } = new ObservableCollection<ProductFile>(); void LoadProductFiles() { if (IsBusyProductFiles) { return; } IsBusyProductFiles = true;

        Task.Run(async () =>
        {                
            var items = await _ProductService.GetProductFiles(SearchModel);
            Application.Current.Dispatcher.Dispatch(() =>
            {                   
                ProductFileList.Clear();                   
                items?.Where(p => p.ProductFileType?.ProductFileTypeAction == ProductFileTypeAction.IsGallery)?
                                    .OrderBy(p => p.Importance)?
                                    .ToList()?.ForEach(p => ProductFileList.Add(p));

                IsBusyProductFiles = false;
            });
        });
    }

Steps to Reproduce

Link to public reproduction project repository

dont have

Version with bug

7.0 (current)

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

Android

Did you find any workaround?

No

Relevant log output

Time	Device Name	Type	PID	Tag	Message
11-12 16:40:11.425	pixel_5_-_api_33	Error	1788	AndroidRuntime	android.runtime.JavaProxyThrowable: System.InvalidOperationException: PlatformView cannot be null here
   at Microsoft.Maui.Handlers.ViewHandler`2[[Microsoft.Maui.IIndicatorView, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Platform.MauiPageControl, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].get_PlatformView()
   at Microsoft.Maui.Handlers.IndicatorViewHandler.OnDisconnectHandler(View platformView)
   at Microsoft.Maui.Handlers.ViewHandler.OnDisconnectHandler(Object platformView)
   at Microsoft.Maui.Handlers.ElementHandler.DisconnectHandler(Object platformView)
   at Microsoft.Maui.Handlers.ElementHandler.Microsoft.Maui.IElementHandler.DisconnectHandler()
   at Microsoft.Maui.Controls.Element.SetHandler(IElementHandler newHandler)
   at Microsoft.Maui.Controls.Element.set_Handler(IElementHandler value)
   at Microsoft.Maui.Controls.VisualElement.Microsoft.Maui.IElement.set_Handler(IElementHandler value)
   at Microsoft.Maui.Platform.ElementExtensions.ToHandler(IElement view, IMauiContext context)
   at Microsoft.Maui.Platform.ElementExtensions.ToPlatform(IElement view, IMauiContext context)
   at Microsoft.Maui.Handlers.LayoutHandler.SetVirtualView(IView view)
   at Microsoft.Maui.Handlers.ViewHandler`2[[Microsoft.Maui.ILayout, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Platform.LayoutViewGroup, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].SetVirtualView(IElement view)
   at Microsoft.Maui.Controls.Element.SetHandler(IElementHandler newHandler)
   at Microsoft.Maui.Controls.Element.set_Handler(IElementHandler value)
   at Microsoft.Maui.Controls.VisualElement.Microsoft.Maui.IElement.set_Handler(IElementHandler value)
   at Microsoft.Maui.Platform.ElementExtensions.ToHandler(IElement view, IMauiContext context)
   at Microsoft.Maui.Platform.ElementExtensions.ToPlatform(IElement view, IMauiContext context)
   at Microsoft.Maui.Handlers.ContentViewHandler.UpdateContent(IContentViewHandler handler)
   at Microsoft.Maui.Handlers.ContentViewHandler.MapContent(IContentViewHandler handler, IContentView page)
   at Microsoft.Maui.PropertyMapper`2.<>c__DisplayClass5_0[[Microsoft.Maui.IContentView, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Handlers.IContentViewHandler, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].<Add>b__0(IElementHandler h, IElement v)
   at Microsoft.Maui.PropertyMapper.UpdatePropertyCore(String key, IElementHandler viewHandler, IElement virtualView)
   at Microsoft.Maui.PropertyMapper.UpdateProperties(IElementHandler viewHandler, IElement virtualView)
   at Microsoft.Maui.Handlers.ElementHandler.SetVirtualView(IElement view)
   at Microsoft.Maui.Handlers.ViewHandler.SetVirtualView(IElement element)
   at Microsoft.Maui.Handlers.ViewHandler`2[[Microsoft.Maui.IContentView, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Platform.ContentViewGroup, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].SetVirtualView(IView view)
   at Microsoft.Maui.Handlers.ContentViewHandler.SetVirtualView(IView view)
   at Microsoft.Maui.Handlers.ViewHandler`2[[Microsoft.Maui.IContentView, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Platform.ContentViewGroup, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].SetVirtualView(IElement view)
   at Microsoft.Maui.Controls.Element.SetHandler(IElementHandler newHandler)
   at Microsoft.Maui.Controls.Element.set_Handler(IElementHandler value)
   at Microsoft.Maui.Controls.VisualElement.Microsoft.Maui.IElement.set_Handler(IElementHandler value)
   at Microsoft.Maui.Platform.ElementExtensions.ToHandler(IElement view, IMauiContext context)
   at Microsoft.Maui.Platform.ViewExtensions.ToHandler(IView view, IMauiContext context)
   at Microsoft.Maui.Controls.Platform.Compatibility.ShellContentFragment.OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
   at AndroidX.Fragment.App.Fragment.n_OnCreateView_Landroid_view_LayoutInflater_Landroid_view_ViewGroup_Landroid_os_Bundle_(IntPtr jnienv, IntPtr native__this, IntPtr native_inflater, IntPtr native_container, IntPtr native_savedInstanceState)
   at Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPLLL_L(_JniMarshal_PPLLL_L callback, IntPtr jnienv, IntPtr klazz, IntPtr p0, IntPtr p1, IntPtr p2)
	at crc640ec207abc449b2ca.ShellContentFragment.n_onCreateView(Native Method)
	at crc640ec207abc449b2ca.ShellContentFragment.onCreateView(ShellContentFragment.java:62)
	at androidx.fragment.app.Fragment.performCreateView(Fragment.java:3104)
	at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:524)
	at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:261)
	at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1890)
	at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1808)
	at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1751)
	at androidx.fragment.app.FragmentManager$5.run(FragmentManager.java:538)
	at android.os.Handler.handleCallback(Handler.java:942)
	at android.os.Handler.dispatchMessage(Handler.java:99)
	at android.os.Looper.loopOnce(Looper.java:201)
	at android.os.Looper.loop(Looper.java:288)
	at android.app.ActivityThread.main(ActivityThread.java:7892)
	at java.lang.reflect.Method.invoke(Native Method)
	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)

Issue Analytics

  • State:closed
  • Created 10 months ago
  • Reactions:2
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
espenrlcommented, Dec 20, 2022

This fixes it for me until it is patched. Based on #12141

           .ConfigureMauiHandlers(handlers =>
            {
#if ANDROID
                handlers.AddHandler(typeof(IndicatorView), typeof(FixCrashIndicatorViewHandler));
#endif
            })

/// <summary>
/// https://github.com/dotnet/maui/pull/12141
/// </summary>
public sealed class FixCrashIndicatorViewHandler : ViewHandler<IIndicatorView, MauiPageControl>, IIndicatorViewHandler
{
    public static readonly IPropertyMapper<IIndicatorView, IIndicatorViewHandler> Mapper = new PropertyMapper<IIndicatorView, IIndicatorViewHandler>(ViewMapper)
    {
        [nameof(IIndicatorView.Count)] = MapCount,
        [nameof(IIndicatorView.Position)] = MapPosition,
        [nameof(IIndicatorView.HideSingle)] = MapHideSingle,
        [nameof(IIndicatorView.MaximumVisible)] = MapMaximumVisible,
        [nameof(IIndicatorView.IndicatorSize)] = MapIndicatorSize,
        [nameof(IIndicatorView.IndicatorColor)] = MapIndicatorColor,
        [nameof(IIndicatorView.SelectedIndicatorColor)] = MapSelectedIndicatorColor,
        [nameof(IIndicatorView.IndicatorsShape)] = MapIndicatorShape
    };

    public static readonly CommandMapper<IIndicatorView, IIndicatorViewHandler> CommandMapper = new(ViewCommandMapper)
    {
    };

    public FixCrashIndicatorViewHandler() : base(Mapper)
    {
    }

    public FixCrashIndicatorViewHandler(IPropertyMapper? mapper)
        : base(mapper ?? Mapper, CommandMapper)
    {
    }

    public FixCrashIndicatorViewHandler(IPropertyMapper? mapper, CommandMapper? commandMapper)
        : base(mapper ?? Mapper, commandMapper ?? CommandMapper)
    {
    }

    IIndicatorView IIndicatorViewHandler.VirtualView => VirtualView;

    MauiPageControl IIndicatorViewHandler.PlatformView => PlatformView;

    protected override MauiPageControl CreatePlatformView()
    {
        return new MauiPageControl(Context);
    }

    protected override void ConnectHandler(MauiPageControl platformView)
    {
        base.ConnectHandler(platformView);

        if (platformView is MauiPageControl pageControl)
            pageControl.SetIndicatorView(VirtualView);
    }

    protected override void DisconnectHandler(MauiPageControl platformView)
    {
        base.DisconnectHandler(platformView);

        if (platformView is MauiPageControl pageControl)
            pageControl.SetIndicatorView(null);
    }

    public static void MapCount(IIndicatorViewHandler handler, IIndicatorView indicator)
    {
        handler.PlatformView.UpdateIndicatorCount();
    }

    public static void MapPosition(IIndicatorViewHandler handler, IIndicatorView indicator)
    {
        handler.PlatformView.UpdatePosition();
    }

    public static void MapHideSingle(IIndicatorViewHandler handler, IIndicatorView indicator)
    {
        handler.PlatformView.UpdateIndicatorCount();
    }

    public static void MapMaximumVisible(IIndicatorViewHandler handler, IIndicatorView indicator)
    {
        handler.PlatformView.UpdateIndicatorCount();
    }

    public static void MapIndicatorSize(IIndicatorViewHandler handler, IIndicatorView indicator)
    {
        handler.PlatformView.ResetIndicators();
    }

    public static void MapIndicatorColor(IIndicatorViewHandler handler, IIndicatorView indicator)
    {
        handler.PlatformView.ResetIndicators();
    }

    public static void MapSelectedIndicatorColor(IIndicatorViewHandler handler, IIndicatorView indicator)
    {
        handler.PlatformView.ResetIndicators();
    }

    public static void MapIndicatorShape(IIndicatorViewHandler handler, IIndicatorView indicator)
    {
        handler.PlatformView.ResetIndicators();
    }
}
0reactions
Kremedcommented, Dec 31, 2022

This fixes it for me until it is patched. Based on #12141

           .ConfigureMauiHandlers(handlers =>
            {
#if ANDROID
                handlers.AddHandler(typeof(IndicatorView), typeof(FixCrashIndicatorViewHandler));
#endif
            }) ```

Works Great For Me

Read more comments on GitHub >

github_iconTop Results From Across the Web

CarouselView crashes Android app when using ...
CarouselView always worked since I started learning .NET MAUI but for some reason now it doesn't work anymore. I haven't done anything strange ......
Read more >
Xamarin.Forms 5.0.0.2612 (5.0.0 Service Release 15) ...
Forms 5 brings to stable release several new controls and features including App Themes (Dark Mode), Brushes, CarouselView, RadioButton, ...
Read more >
Display Rich Data with CollectionView - .NET MAUI Tutorial ...
This video is part of a crash course to build your first .NET MAUI app. The next part in this tutorial is ready...
Read more >
net maui Archives
ItemsSource> </CarouselView> <IndicatorView x:Name="indicatorView" Grid. ... After having put down the migration for a second time, I picked it up again ...
Read more >
.NET MAUI android application crashes immediately after ...
I've run into this problem several times and it was always fixable by looking at the logs. Upvote 1
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found