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

NullReferenceException in ShellToolbarAppearanceTracker.SetColors on Android #12460

Closed
BioTurboNick opened this issue Jan 6, 2023 · 5 comments · Fixed by #12539
Closed

NullReferenceException in ShellToolbarAppearanceTracker.SetColors on Android #12460

BioTurboNick opened this issue Jan 6, 2023 · 5 comments · Fixed by #12539
Labels
area-controls-shell Shell Navigation, Routes, Tabs, Flyout fixed-in-8.0.0-preview.1.7762 Look for this fix in 8.0.0-preview.1.7762! platform/android 🤖 t/bug Something isn't working
Milestone

Comments

@BioTurboNick
Copy link
Contributor

BioTurboNick commented Jan 6, 2023

Description

Had 3 reports of crashes in this method from two devices, on Android 9 and 10, via AppCenter

Here's the code in question:

protected virtual void SetColors(AToolbar toolbar, IShellToolbarTracker toolbarTracker, Color foreground, Color background, Color title)
{
_shellContext.Shell.Toolbar.BarTextColor = title ?? ShellRenderer.DefaultTitleColor;
_shellContext.Shell.Toolbar.BarBackground = new SolidColorBrush(background ?? ShellRenderer.DefaultBackgroundColor);
_shellContext.Shell.Toolbar.IconColor = foreground ?? ShellRenderer.DefaultForegroundColor;
}

Is the MAUI library using nullable types? If not, there's no verification that _shellContext is not null when the object is constructed. And if it's at all possible to be accessed after being disposed, that would also produce this exception.

Even if _shellContext is not null, is .Shell, or .Shell.Toolbar guaranteed to not be null?

Steps to Reproduce

Unknown

Link to public reproduction project repository

n/a

Version with bug

7.0 (current)

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

Android 9-10

Did you find any workaround?

No response

Relevant log output

Full stack trace:

Microsoft.Maui.Controls.Platform.Compatibility.ShellToolbarAppearanceTracker.SetColors(Toolbar toolbar, IShellToolbarTracker toolbarTracker, Color foreground, Color background, Color title)
Microsoft.Maui.Controls.Platform.Compatibility.ShellToolbarAppearanceTracker.SetAppearance(Toolbar toolbar, IShellToolbarTracker toolbarTracker, ShellAppearance appearance)
Microsoft.Maui.Controls.Platform.Compatibility.ShellSectionRenderer.SetAppearance(ShellAppearance appearance)
Microsoft.Maui.Controls.Platform.Compatibility.ShellSectionRenderer.Microsoft.Maui.Controls.IAppearanceObserver.OnAppearanceChanged(ShellAppearance appearance)
Microsoft.Maui.Controls.Shell.Microsoft.Maui.Controls.IShellController.AppearanceChanged(Element source, Boolean appearanceSet)
Microsoft.Maui.Controls.ShellSection.SendAppearanceChanged()
Microsoft.Maui.Controls.ShellSection.UpdateDisplayedPage()
Microsoft.Maui.Controls.ShellSection.OnVisibleChildAdded(Element child)
Microsoft.Maui.Controls.ShellSection.OnChildAdded(Element child)
Microsoft.Maui.Controls.BaseShellItem.AddLogicalChild(Element element)
Microsoft.Maui.Controls.ShellSection.AddPage(Page page)
Microsoft.Maui.Controls.ShellSection.OnPushAsync(Page page, Boolean animated)
Microsoft.Maui.Controls.ShellSection.PushStackOfPages(List`1 pages, Nullable`1 animate)
Microsoft.Maui.Controls.ShellSection.GoToAsync(ShellNavigationRequest request, ShellRouteParameters queryData, IServiceProvider services, Nullable`1 animate, Boolean isRelativePopping)
Microsoft.Maui.Controls.ShellNavigationManager.GoToAsync(ShellNavigationParameters shellNavigationParameters, ShellNavigationRequest navigationRequest)
@BioTurboNick BioTurboNick added the t/bug Something isn't working label Jan 6, 2023
@mattleibow mattleibow added the area-controls-shell Shell Navigation, Routes, Tabs, Flyout label Jan 6, 2023
@mattleibow mattleibow added this to the Backlog milestone Jan 6, 2023
@ghost
Copy link

ghost commented Jan 6, 2023

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

@PureWeen
Copy link
Member

@BioTurboNick those should never be null so this probably indicates that something isn't unwiring correctly further up the chain. Can you provide some added context about the structure of your app?

  1. do you ever swap out the main page or is it always just shell?
  2. if the user backgrounds the app and then foregrounds it does shell get recreated?
  3. what does your appshell xaml look like?
  4. Do you have any ideas what that GotoAsync call is doing? Is it just pushing pages? is it switching to a different shell element?

@BioTurboNick
Copy link
Contributor Author

  1. Always just Shell
  2. It shouldn't, haven't touched the default creation/suspend/restore logic
  3. See below
  4. I'm not sure why the name of the function I'm actually calling is dropped, but this where it must be invoked:
await AppShell.Current.Navigation.PushAsync(new HemocytometerPage(pageViewModel));

This line is present in the Execute method of the command invoked when one of the MenuItem entries is clicked.

<?xml version="1.0" encoding="UTF-8" ?>
<Shell
    x:Name="root"
    x:Class="HemocytometerSidekick.AppShell"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:local="clr-namespace:HemocytometerSidekick"
    xmlns:views="clr-namespace:HemocytometerSidekick.Views"
    xmlns:strings="clr-namespace:HemocytometerSidekick.Resources.Strings"
    FlyoutBackgroundColor="{AppThemeBinding Dark={StaticResource DarkBackgroundColor}}">

    <Shell.FlyoutHeader>
        <Grid
            BackgroundColor="{AppThemeBinding Light={StaticResource Primary}, Dark={StaticResource PrimaryMediumDark}}"
            ColumnDefinitions="10,80,*,10"
            RowSpacing="5"
            RowDefinitions="30,80,Auto"
            Padding="0, 0, 0, 10">

            <Image
                Grid.Row="1"
                Grid.Column="1"
                Source="menulogo.png" />

            <Label
                Grid.Column="1"
                Grid.Row="2"
                Grid.ColumnSpan="2"
                TextColor="White"
                FontSize="20"
                Text="{x:Static strings:AppResources.AppName}" />
            
        </Grid>
    </Shell.FlyoutHeader>

    <ShellContent
        Title="{x:Static strings:AppResources.Home}"
        ContentTemplate="{DataTemplate views:IntroPage}"
        Route="IntroPage">
        <ShellContent.Icon>
            <FontImageSource
                FontFamily="SegoeFluentIcons"
                Color="{AppThemeBinding Light={StaticResource Gray900}, Dark={StaticResource DarkThemeLightTextColor}}"
                Glyph="&#xE80F;" />
        </ShellContent.Icon>
    </ShellContent>

    <MenuItem
        Text="{x:Static strings:AppResources.Hemocytometer}"
        BindingContext="{x:Reference root}"
        Clicked="Hemocytometer_Clicked">
        <MenuItem.IconImageSource>
            <FontImageSource
                FontFamily="SegoeFluentIcons"
                Color="{AppThemeBinding Light={StaticResource Gray900}, Dark={StaticResource DarkThemeLightTextColor}}"
                Glyph="&#xE8EF;" />
        </MenuItem.IconImageSource>
    </MenuItem>

    <MenuItem
        Text="{x:Static strings:AppResources.BloodDifferential}"
        BindingContext="{x:Reference root}"
        Clicked="BloodDifferential_Clicked">
        <MenuItem.IconImageSource>
            <FontImageSource
                FontFamily="SegoeFluentIcons"
                Color="{AppThemeBinding Light={StaticResource Gray900}, Dark={StaticResource DarkThemeLightTextColor}}"
                Glyph="&#xE8EF;" />
        </MenuItem.IconImageSource>
    </MenuItem>
    
    <ShellContent
        Title="{x:Static strings:AppResources.Results}"
        ContentTemplate="{DataTemplate views:ResultsPage}">
        <ShellContent.Icon>
            <FontImageSource
                FontFamily="SegoeFluentIcons"
                Color="{AppThemeBinding Light={StaticResource Gray900}, Dark={StaticResource DarkThemeLightTextColor}}"
                Glyph="&#xE8BC;" />
        </ShellContent.Icon>
    </ShellContent>

    <MenuItem
        Text="{x:Static strings:AppResources.Feedback}"
        BindingContext="{x:Reference root}"
        Command="{Binding SendFeedbackCommand}">
        <MenuItem.IconImageSource>
            <FontImageSource
                FontFamily="SegoeFluentIcons"
                Color="{AppThemeBinding Light={StaticResource Gray900}, Dark={StaticResource DarkThemeLightTextColor}}"
                Glyph="&#xE939;" />
        </MenuItem.IconImageSource>
    </MenuItem>

    <ShellContent
        Title="{x:Static strings:AppResources.Settings}"
        BindingContext="{x:Reference root}"
        ContentTemplate="{DataTemplate views:SettingsPage}">
        <ShellContent.Icon>
            <FontImageSource
                FontFamily="SegoeFluentIcons"
                Color="{AppThemeBinding Light={StaticResource Gray900}, Dark={StaticResource DarkThemeLightTextColor}}"
                Glyph="&#xE713;" />
        </ShellContent.Icon>
    </ShellContent>

    <MenuItem
        Text="{x:Static strings:AppResources.MoreInformation}"
        BindingContext="{x:Reference root}"
        Command="{Binding MoreInformationCommand}">
        <MenuItem.IconImageSource>
            <FontImageSource
                FontFamily="SegoeFluentIcons"
                Color="{AppThemeBinding Light={StaticResource Gray900}, Dark={StaticResource DarkThemeLightTextColor}}"
                Glyph="&#xE946;" />
        </MenuItem.IconImageSource>
    </MenuItem>

</Shell>

@BioTurboNick
Copy link
Contributor Author

Some other crashes recorded by AppCenter definitely point to something wrong with disposal:

Microsoft.Maui.Controls.Platform.Compatibility.ShellToolbarTracker.Dispose(Boolean disposing)
Java.Lang.Object.Java.Interop.IJavaPeerable.Disposed()
Java.Interop.JniRuntime.JniValueManager.DisposePeer(IJavaPeerable )
Java.Lang.Object.Dispose()
Microsoft.Maui.Controls.Platform.Compatibility.ShellSectionRenderer.Destroy()
Microsoft.Maui.Controls.Platform.Compatibility.ShellSectionRenderer.OnDestroy()
AndroidX.Fragment.App.Fragment.n_OnDestroy(IntPtr , IntPtr )
Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PP_V(_JniMarshal_PP_V , IntPtr , IntPtr )

@JoacimWall
Copy link

Found a workaround that works for me.
App.xaml.cs
protected async override void OnResume()
{
if (DeviceInfo.Platform == DevicePlatform.Android)
{
Shell.SetBackgroundColor(this, AppColors.Primary500Color);
Shell.SetForegroundColor(this, AppColors.WhiteColor);
Shell.SetTabBarBackgroundColor(this, AppColors.Primary800Color);
Shell.SetTabBarForegroundColor(this, AppColors.PrimaryAccentColor);
Shell.SetTabBarUnselectedColor(this, AppColors.WhiteColor);
Shell.SetTabBarTitleColor(this, AppColors.PrimaryAccentColor);
}
}

@ghost ghost locked as resolved and limited conversation to collaborators Apr 13, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-controls-shell Shell Navigation, Routes, Tabs, Flyout fixed-in-8.0.0-preview.1.7762 Look for this fix in 8.0.0-preview.1.7762! platform/android 🤖 t/bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants