Repository navigation
Closed
Labels
area-controls-collectionviewCollectionView, CarouselView, IndicatorViewfixed-in-8.0.0-preview.1.7762Look for this fix in 8.0.0-preview.1.7762!p/1Work that is important, and has been scheduled for release in this or an upcoming sprintplatform/androidt/bugSomething isn't working
Milestone
Description
Description
An Exception occurs when adding item to ObservableCollection with an EmptyView after having navigated to the same page for a second time.
System.InvalidOperationException: 'VirtualView cannot be null here'
The exception occurs:
- Only on Android (works fine on Windows)
- Only when navigating to the page a second (or third...) time
- I tried using VS 17.4.3, as well as 17.5.0 Preview 2.0, both latest as of today
maui-android 7.0.52/7.0.100 VS 17.4.33205.214, VS 17.5.33209.295 - Using both
CollectionView EmptyView="something"as well as using separate<CollectionView.EmptyView><ContentView>...
Steps to Reproduce
- Create a File > New .NET MAUI App
- Add a second ContentPage to the project, named "Page2"
- In AppShell.xaml.cs add:
Routing.RegisterRoute("Page2", typeof(Page2)); - In MauiProgram.cs add:
builder.Services.AddScoped<Page2>(); - In Page2.xaml remove the
Labeland replace with:
<CollectionView ItemsSource="{Binding Values}">
<CollectionView.EmptyView>
<ContentView>
<Label Text="No items"
FontAttributes="Bold" />
</ContentView>
</CollectionView.EmptyView>
<CollectionView.ItemTemplate>
<DataTemplate x:DataType="x:String">
<Label Text="{Binding .}" />
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
<Button
Text="Load items"
Pressed="Button_Pressed" />
- In Page2.xaml.cs replace the class with:
public partial class Page2 : ContentPage
{
public ObservableCollection<string> Values { get; set; } = new();
public Page2()
{
InitializeComponent();
this.BindingContext = this;
}
private async void Button_Pressed(object sender, EventArgs e)
{
Values.Clear();
await Task.Delay(1000);
for (int i = 1; i <= 5; i++)
Values.Add($"Item #{i}");
}
}
- In MainPage.xaml.cs modify
OnCounterClickedas follows:
private async void OnCounterClicked(object sender, EventArgs e)
{
count++;
if (count == 1)
CounterBtn.Text = $"Clicked {count} time";
else
CounterBtn.Text = $"Clicked {count} times";
SemanticScreenReader.Announce(CounterBtn.Text);
await Shell.Current.GoToAsync("/Page2");
}
- Run the app on Android
- Click the button on MainPage
- Click the button on Page2 two or more times --> works fine
- Go back to MainPage
- Click the button on MainPage again
- Click the button on Page2 --> Exception occurrs:
System.InvalidOperationException: VirtualView cannot be null here at Microsoft.Maui.Handlers.ViewHandler`2[[Microsoft.Maui.Controls.ReorderableItemsView, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[AndroidX.RecyclerView.Widget.RecyclerView, Xamarin.AndroidX.RecyclerView, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].get_VirtualView() in D:\a\_work\1\s\src\Core\src\Handlers\View\ViewHandlerOfT.cs:line 42 at Microsoft.Maui.Controls.Handlers.Items.StructuredItemsViewHandler`1[[Microsoft.Maui.Controls.ReorderableItemsView, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].GetItemsLayout() in D:\a\_work\1\s\src\Controls\src\Core\Handlers\Items\StructuredItemsViewHandler.Android.cs:line 10 at Microsoft.Maui.Controls.Handlers.Items.MauiRecyclerView`3[[Microsoft.Maui.Controls.ReorderableItemsView, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Controls.Handlers.Items.GroupableItemsViewAdapter`2[[Microsoft.Maui.Controls.ReorderableItemsView, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Controls.Handlers.Items.IGroupableItemsViewSource, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Controls.Handlers.Items.IGroupableItemsViewSource, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].UpdateLayoutManager() in D:\a\_work\1\s\src\Controls\src\Core\Handlers\Items\Android\MauiRecyclerView.cs:line 288 at Microsoft.Maui.Controls.Handlers.Items.MauiRecyclerView`3[[Microsoft.Maui.Controls.ReorderableItemsView, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Controls.Handlers.Items.GroupableItemsViewAdapter`2[[Microsoft.Maui.Controls.ReorderableItemsView, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Controls.Handlers.Items.IGroupableItemsViewSource, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Controls.Handlers.Items.IGroupableItemsViewSource, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].UpdateEmptyViewVisibility() in D:\a\_work\1\s\src\Controls\src\Core\Handlers\Items\Android\MauiRecyclerView.cs:line 552 at Microsoft.Maui.Controls.Handlers.Items.DataChangeObserver.OnItemRangeInserted(Int32 positionStart, Int32 itemCount) in D:\a\_work\1\s\src\Controls\src\Core\Handlers\Items\Android\DataChangeObserver.cs:line 53 at AndroidX.RecyclerView.Widget.RecyclerView.AdapterDataObserver.n_OnItemRangeInserted_II(IntPtr jnienv, IntPtr native__this, Int32 positionStart, Int32 itemCount) in C:\a\_work\3\s\generated\androidx.recyclerview.recyclerview\obj\Release\net6.0-android\generated\src\AndroidX.RecyclerView.Widget.RecyclerView.cs:line 1124 at Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPII_V(_JniMarshal_PPII_V callback, IntPtr jnienv, IntPtr klazz, Int32 p0, Int32 p1) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:line 154
Link to public reproduction project repository
https://github.com/jaldinger/MauiAppRepro1.App
Version with bug
7.0
Last version that worked well
Unknown/Other
Affected platforms
Android
Affected platform versions
Android (tested 8.1)
Did you find any workaround?
Removing the EmptyView from the CollectionView avoids the exception (but removes the EmtpyView).
Relevant log output
No response
Reacted by Alex Monahan, Nicholas Bauer, 7702244, Steve Hansen, Tuukka Viitanen, Teet Triisa, Sergtek, Imran Sh and LiviuShiva
Metadata
Metadata
Assignees
Labels
area-controls-collectionviewCollectionView, CarouselView, IndicatorViewfixed-in-8.0.0-preview.1.7762Look for this fix in 8.0.0-preview.1.7762!p/1Work that is important, and has been scheduled for release in this or an upcoming sprintplatform/androidt/bugSomething isn't working
Activity
canperk commentedon Dec 20, 2022
I solved this problem by adding Task.Delay(50) when adding new item to Collection. This is not the best solutşon but for now it is enough for me
jaldinger commentedon Dec 20, 2022
I tried delays in several places, but it didn't fix anything for me. My code has a delay, but only for demonstration purposes....
BioTurboNick commentedon Dec 25, 2022
Just encountered this myself.
lucianparvu commentedon Jan 17, 2023
Same Problem here too
JkPersson commentedon Jan 27, 2023
Also seeing it in a CollectionView with no EmptyView!
lucianparvu commentedon Jan 27, 2023
I tried some workarround to replace existing items and now it works ( without clear )
jerry08 commentedon Jan 27, 2023
I experience this issue a lot so I made a workaround using a custom handler. Here's my code:
And in
MauiProgram.cs, configure the handlerIf you need more workarounds (including this one), I made a small project here.
7702244 commentedon Jan 28, 2023
@jerry08 Why don't you just send a Pull Request with your workarounds and help the community make MAUI better?
jerry08 commentedon Jan 28, 2023
@7702244 Pull requests were already submitted for some but were closed since the team is focusing on other matters. That's why I posted some workarounds in the meanwhile.
6 remaining items
[Android] Fix crash adding items to CollectionView on navigating (#13385
Merge net8.0 into release/8.0.1xx-preview1 (#13441)
[Android] Fix crash adding items to CollectionView on navigating (dot…
LennoxP90 commentedon Mar 10, 2023
I would like to express my hopes this will make it into the next SR for 7.0