Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crash on iOS when initializing a TabbedPage without children #22633

Closed
superwilly1 opened this issue May 24, 2024 · 4 comments · Fixed by #22644
Closed

Crash on iOS when initializing a TabbedPage without children #22633

superwilly1 opened this issue May 24, 2024 · 4 comments · Fixed by #22644
Labels
area-controls-tabbedpage TabbedPage fixed-in-8.0.60 fixed-in-9.0.0-preview.5.24307.10 i/regression This issue described a confirmed regression on a currently supported version platform/iOS 🍎 t/bug Something isn't working
Milestone

Comments

@superwilly1
Copy link

Description

In our app we use a TabbedPage.
We have a NavigationService that dynamically adds pages to this TabbedPage.
We start with an empty TabbedPage, the Child pages are added asynchronously during startup.
This causes a crash on iOS in version 8.0.40.
On Android it does still work.

Steps to Reproduce

Create an empty TabbedPage like this:

<?xml version="1.0" encoding="utf-8" ?>
<TabbedPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="ListViewScroll.TestTabbedPage"
             Title="TestTabbedPage">
</TabbedPage>

Set this TestTabbedPage as the MainPage in app.xaml.cs:

public partial class App : Application
{
	public App()
	{
		InitializeComponent();
		MainPage = new TestTabbedPage();
	}
}

When you run this now, on iOS this will cause a crash (Android will not crash and show an empty TabbedPage).

To simulate our NavigationService that will asynchronously add child pages to the TabbedPage, use this in the codebehind:

public partial class TestTabbedPage : TabbedPage
{
    public TestTabbedPage() => InitializeComponent();

    protected override void OnAppearing()
    {
        base.OnAppearing();
        _ = SimulateNavigationServiceAsync();
    }

    async Task SimulateNavigationServiceAsync()
    {
        // add a delay to simulate the navigation service
        await Task.Delay(100);
        // add child pages to the TabbedPage
        Children.Add(new ContentPage { Title = "Page1" });
        Children.Add(new ContentPage { Title = "Page2" });
    }
}

The app crashes on iOS, on Android it does work (showing a tabbedpage with the 2 child pages).

Link to public reproduction project repository

No response

Version with bug

8.0.40 SR5

Is this a regression from previous behavior?

Yes, this used to work in .NET MAUI

Last version that worked well

8.0.21 SR4.1

Affected platforms

iOS

Affected platform versions

iOS 17.4.1

Did you find any workaround?

As a workaround, add a dummy child page and delete it when adding the pages from the NavigationService:

public partial class TestTabbedPage : TabbedPage
{
    public TestTabbedPage() => InitializeComponent();

    protected override void OnAppearing()
    {
        base.OnAppearing();
        // add a dummy child page to avoid the app from crashing on iOS
        Children.Add(new ContentPage());
        _ = SimulateNavigationServiceAsync();
    }

    async Task SimulateNavigationServiceAsync()
    {
        // add a delay to simulate the navigation service
        await Task.Delay(100);
        // delete the dummy page
        Children.RemoveAt(0);
        // add child pages to the TabbedPage
        Children.Add(new ContentPage { Title = "Page1" });
        Children.Add(new ContentPage { Title = "Page2" });
    }
}

Relevant log output

System.NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.Maui.Controls.Handlers.Compatibility.TabbedRenderer.UpdatePageSpecifics()
   at Microsoft.Maui.Controls.Handlers.Compatibility.TabbedRenderer.SetElement(VisualElement element)
   at Microsoft.Maui.Controls.Handlers.Compatibility.TabbedRenderer.Microsoft.Maui.IElementHandler.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.Platform.ElementExtensions.ToUIViewController(IElement view, IMauiContext context)
   at Microsoft.Maui.Handlers.WindowHandler.MapContent(IWindowHandler handler, IWindow window)
   at Microsoft.Maui.PropertyMapper`2.<>c__DisplayClass5_0[[Microsoft.Maui.IWindow, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Handlers.IWindowHandler, 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.Controls.Element.SetHandler(IElementHandler newHandler)
   at Microsoft.Maui.Controls.Element.set_Handler(IElementHandler value)
   at Microsoft.Maui.Platform.ElementExtensions.SetHandler(INativeObject nativeElement, IElement element, IMauiContext context)
   at Microsoft.Maui.Platform.ElementExtensions.SetWindowHandler(UIWindow platformWindow, IWindow window, IMauiContext context)
   at Microsoft.Maui.Platform.ApplicationExtensions.CreatePlatformWindow(IApplication application, UIWindowScene windowScene, NSDictionary[] states)
   at Microsoft.Maui.Platform.ApplicationExtensions.CreatePlatformWindow(IUIApplicationDelegate platformApplication, IApplication application, UIApplication uiApplication, NSDictionary launchOptions)
   at Microsoft.Maui.MauiUIApplicationDelegate.FinishedLaunching(UIApplication application, NSDictionary launchOptions)
   at UIKit.UIApplication.UIApplicationMain(Int32 argc, String[] argv, IntPtr principalClassName, IntPtr delegateClassName) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/UIKit/UIApplication.cs:line 58
   at UIKit.UIApplication.Main(String[] args, Type principalClass, Type delegateClass) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/UIKit/UIApplication.cs:line 94
   at ListViewScroll.Program.Main(String[] args) in /Users/wilfred/Projects/ListViewScroll/Platforms/iOS/Program.cs:line 13
@superwilly1 superwilly1 added the t/bug Something isn't working label May 24, 2024
Copy link
Contributor

Hi I'm an AI powered bot that finds similar issues based off the issue title.

Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one and thumbs upping the other issue to help us prioritize it. Thank you!

Open similar issues:

Closed similar issues:

Note: You can give me feedback by thumbs upping or thumbs downing this comment.

@jfversluis jfversluis added platform/iOS 🍎 area-controls-tabbedpage TabbedPage i/regression This issue described a confirmed regression on a currently supported version labels May 24, 2024
@rmarinho rmarinho added this to the Backlog milestone May 24, 2024
@PureWeen PureWeen modified the milestones: Backlog, .NET 8 SR6 May 24, 2024
kubaflo added a commit to kubaflo/maui that referenced this issue May 24, 2024
PureWeen pushed a commit that referenced this issue May 24, 2024
@MitchBomcanhao
Copy link

same problem here. quite a critical issue for us.

@mrwcjoughinaaf
Copy link

this IS an issue and it is not the same as the ones your bot found - please reopen this issue!
I litterally just experienced this issue and am going to have to work around it by having one tab in the tabbedpage to start off with - even though they are dynamically added at runtime.

@jfversluis @mattleibow

@konradzuse
Copy link

also getting this crash on iOS 8.0.40

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-controls-tabbedpage TabbedPage fixed-in-8.0.60 fixed-in-9.0.0-preview.5.24307.10 i/regression This issue described a confirmed regression on a currently supported version platform/iOS 🍎 t/bug Something isn't working
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

8 participants