maui
maui copied to clipboard
[android] Identified crash in toolbar extensions when using a custom image source
Description
On Android, when using a custom image source as the source for an IconImageSource of a Toolbar item, and that source does not implement GetConstantState, maui crashes:
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:svg="https://vapolia.eu/svg">
<ContentPage.ToolbarItems>
<ToolbarItem Text="{x:Static appresource:AppResources.Home_Filter}"
Command="{Binding OpenFilterViewCommand}"
Order="Primary"
IconImageSource="{svg:Svg my_icon.svg}" />
</ContentPage.ToolbarItems>
Steps to Reproduce
The issue is caused by the code in https://github.com/dotnet/maui/blob/main/src/Controls/src/Core/Platform/Android/Extensions/ToolbarExtensions.cs
which ignores the null value returned by baseDrawable.GetConstantState()
as not all custom drawable implements that caching feature.
Instead, it should not ignore the null value, and provides an alternate implementation when GetConstantState() returns null
Link to public reproduction project repository
No response
Version with bug
8.0.21 SR4.1
Is this a regression from previous behavior?
Yes, this used to work in Xamarin.Forms
Last version that worked well
Unknown/Other
Affected platforms
Android
Affected platform versions
Android all versions
Did you find any workaround?
no
Relevant log output
04-30 10:56:46.028 I/DOTNET (19040): 97|2024-04-30T10:56:46.0278601+00:00|ERROR|1|IImageSource|Unexpected exception in LoadImage. --> System.NullReferenceException: Object reference not set to an instance of an object.
04-30 10:56:46.028 I/DOTNET (19040): at Microsoft.Maui.Controls.Platform.ToolbarExtensions.<>c__DisplayClass13_0.<UpdateMenuItemIcon>b__0(IImageSourceServiceResult`1 result) in D:\a\_work\1\s\src\Controls\src\Core\Platform\Android\Extensions\ToolbarExtensions.cs:line 366
04-30 10:56:46.028 I/DOTNET (19040): at Microsoft.Maui.ImageSourceExtensions.LoadImageResult(Task`1 task, Action`1 finished) in D:\a\_work\1\s\src\Core\src\ImageSources\ImageSourceExtensions.cs:line 32
04-30 10:56:46.028 I/DOTNET (19040): at Microsoft.Maui.TaskExtensions.FireAndForget(Task task, Action`1 errorCallback) in D:\a\_work\1\s\src\Core\src\TaskExtensions.cs:line 36
/similarissues
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:
- [Android] ToolbarItem shows incorrect image on different pages on initial load when used in Shell (#19950), similarity score: 0.75
- NullReferenceException in UpdateLeftBarButtonItem (#14801), similarity score: 0.75
- Android Crash ImageSource Glyph (#12513), similarity score: 0.74
Closed similar issues:
- NavigationPage.SetTitleIconImageSource Cases crash on iOS (#21518), similarity score: 0.76
- [.net7][iOS] IconImageSource on TabbedPage Causes Crash [MAUI Blazor] [Related #12403] (#14850), similarity score: 0.73
Note: You can give me feedback by thumbs upping or thumbs downing this comment.
The repro is in the comment. Along with the line where the bug is.
@softlion can you attach a full project please?
Man, the issue is so obvious - I pointed the exact line in the maui source code - that i won't spend 1 more hour on it.
The maui code voluntarily ignores the null return value with an exclamation mark. If it can return null, why ignore it ?
I'm the publisher of a custom component that creates a full custom imagesource with a custom android Drawable.
I already updated my component so it now implements that optional Drawable override, So that issue is worked around.
So that issue is worked around.
Good to hear!
So that issue is worked around.
Good to hear!
Do you have a financial bonus when you close an issue ?
I would like to understand why you dont add a if( xxxx is not null )
in the source code instead of leaving that bug there and closing that issue ?