NullReferenceException in CollectionView
Closed this issue · 4 comments
Platform:
- OS: Android
- Device: Samsung S20 Ultra 5G
- Sdk vervion: Android 12 (API 31)
- Xamarin.Forms: [5.0.0.2337]
- Sharpnado.CollectionView: 2.0.0
- Sharpnado.Shadows: 1.2.0
- Sharpnado.Tabs:2.2.0
Describe the bug
When i use the Horizontal List View with ObservableCollection, or List, as ItemSource the ListView return a NullReferenceException.
I already tried in various implementetion, with CollectionView(Sharpnado, not the standard), but in all implemention i have the same error.
If i remove the implementetion of HorizontalListView and insert a CollectionView (standard) the application continue correctly.
To Reproduce
Steps to reproduce the behavior:
ViewModel:
using Microsoft.Extensions.Logging;
using App.Infrastructure.ApiInterface;
using App.Models.Filters;
using App.Models.Ticket;
using Sharpnado.CollectionView.Paging;
using Sharpnado.CollectionView.Services;
using Sharpnado.CollectionView.ViewModels;
using Sharpnado.Presentation.Forms;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Xamarin.CommunityToolkit.UI.Views;
namespace App.ViewModels
{
public class DashboardViewModel : BaseViewModel
{
private readonly ILogger<DashboardViewModel> _logger;
private readonly ITicketsApi _ticketsApi;
private LayoutState groupState;
public ObservableCollection<TicketTable> Tickets { get; set; }
public LayoutState GroupState
{
get => groupState;
set => SetProperty(ref groupState, value);
}
public DashboardViewModel(ILogger<DashboardViewModel> logger, ITicketsApi ticketsApi)
{
_logger = logger;
_ticketsApi = ticketsApi;
Title = "Dashboard";
Tickets = new();
}
public async void InitViewModel()
{
try
{
GroupState = LayoutState.Loading;
Tickets = new(await _ticketsApi.GetTicketsAsync());
OnPropertyChanged(nameof(Tickets));
GroupState = LayoutState.Success;
}
catch (Exception e)
{
_logger.LogError(e, MethodBase.GetCurrentMethod().Name);
}
}
}
}
View:
<?xml version="1.0" encoding="UTF-8" ?>
<ContentView
x:Class="App.Views.DashboardView"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:loader="clr-namespace:Sharpnado.Presentation.Forms.CustomViews;assembly=Sharpnado.TaskLoaderView"
xmlns:models="clr-namespace:App.Models"
xmlns:shv="http://sharpnado.com"
xmlns:viewmodels="clr-namespace:App.ViewModels"
xmlns:xct="http://xamarin.com/schemas/2020/toolkit"
x:DataType="viewmodels:DashboardViewModel"
>
<ContentView.Content>
<StackLayout xct:StateLayout.CurrentState="{Binding GroupState}">
<xct:StateLayout.StateViews>
<xct:StateView StateKey="Success">
<StackLayout>
<Label Text="Dashboard" TextColor="Black" />
<shv:HorizontalListView
Grid.Row="3"
Margin="-16,8"
CollectionPadding="0,8"
ItemHeight="144"
ItemSpacing="8"
ItemWidth="144"
ItemsSource="{Binding Tickets}"
SnapStyle="Center"
>
<shv:HorizontalListView.ItemTemplate>
<DataTemplate x:DataType="models:TicketTable">
<ViewCell>
<Label Text="{Binding TicketName}" />
</ViewCell>
</DataTemplate>
</shv:HorizontalListView.ItemTemplate>
</shv:HorizontalListView>
</StackLayout>
</xct:StateView>
<xct:StateView StateKey="Loading">
<StackLayout>
<ActivityIndicator
IsEnabled="True"
IsRunning="True"
Color="{DynamicResource Primary}"
/>
</StackLayout>
</xct:StateView>
</xct:StateLayout.StateViews>
</StackLayout>
</ContentView.Content>
</ContentView>
View.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace App.Views
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class DashboardView : ContentView
{
private readonly DashboardViewModel ViewModel = Startup.ServiceProvider.GetService<DashboardViewModel>();
public DashboardView()
{
InitializeComponent();
BindingContext = ViewModel;
}
protected override void OnBindingContextChanged()
{
base.OnBindingContextChanged();
ViewModel.InitViewModel();
}
}
Exceptions (if applicable)
Object reference not set to an instance of an object.
Sharpnado.CollectionView.Droid
at Sharpnado.CollectionView.Droid.Renderers.SpaceItemDecoration..ctor (System.Int32 spaceDp) [0x00006] in D:\Dev\Sharpnado\src\Sharpnado.HorizontalListView\Sharpnado.CollectionView.Droid\Renderers\ResponsiveGridLayoutManager.cs:31
at Sharpnado.CollectionView.Droid.Renderers.CollectionViewRenderer.SetListLayout (AndroidX.RecyclerView.Widget.RecyclerView recyclerView) [0x00099] in D:\Dev\Sharpnado\src\Sharpnado.HorizontalListView\Sharpnado.CollectionView.Droid\Renderers\CollectionViewRenderer.cs:251
at Sharpnado.CollectionView.Droid.Renderers.CollectionViewRenderer.CreateView (Sharpnado.CollectionView.RenderedViews.CollectionView collection) [0x00038] in D:\Dev\Sharpnado\src\Sharpnado.HorizontalListView\Sharpnado.CollectionView.Droid\Renderers\CollectionViewRenderer.cs:198
at Sharpnado.CollectionView.Droid.Renderers.CollectionViewRenderer.OnElementChanged (Xamarin.Forms.Platform.Android.ElementChangedEventArgs`1[TElement] e) [0x000bb] in D:\Dev\Sharpnado\src\Sharpnado.HorizontalListView\Sharpnado.CollectionView.Droid\Renderers\CollectionViewRenderer.cs:89
at Xamarin.Forms.Platform.Android.VisualElementRenderer`1[TElement].SetElement (TElement element) [0x000cb] in D:\a\1\s\Xamarin.Forms.Platform.Android\VisualElementRenderer.cs:241
at Xamarin.Forms.Platform.Android.VisualElementRenderer`1[TElement].Xamarin.Forms.Platform.Android.IVisualElementRenderer.SetElement (Xamarin.Forms.VisualElement element) [0x00033] in D:\a\1\s\Xamarin.Forms.Platform.Android\VisualElementRenderer.cs:131
at Xamarin.Forms.Platform.Android.Platform.CreateRenderer (Xamarin.Forms.VisualElement element, Android.Content.Context context) [0x0003e] in D:\a\1\s\Xamarin.Forms.Platform.Android\Platform.cs:358
at Xamarin.Forms.Platform.Android.VisualElementPackager.AddChild (Xamarin.Forms.VisualElement view, Xamarin.Forms.Platform.Android.IVisualElementRenderer oldRenderer, Xamarin.Forms.Platform.Android.RendererPool pool, System.Boolean sameChildren) [0x000b3] in D:\a\1\s\Xamarin.Forms.Platform.Android\VisualElementPackager.cs:140
at Xamarin.Forms.Platform.Android.VisualElementPackager.SetElement (Xamarin.Forms.VisualElement oldElement, Xamarin.Forms.VisualElement newElement) [0x00163] in D:\a\1\s\Xamarin.Forms.Platform.Android\VisualElementPackager.cs:351
at Xamarin.Forms.Platform.Android.VisualElementPackager.Load () [0x00000] in D:\a\1\s\Xamarin.Forms.Platform.Android\VisualElementPackager.cs:112
at Xamarin.Forms.Platform.Android.VisualElementRenderer`1[TElement].SetPackager (Xamarin.Forms.Platform.Android.VisualElementPackager packager) [0x00007] in D:\a\1\s\Xamarin.Forms.Platform.Android\VisualElementRenderer.cs:481
at Xamarin.Forms.Platform.Android.VisualElementRenderer`1[TElement].SetElement (TElement element) [0x000e8] in D:\a\1\s\Xamarin.Forms.Platform.Android\VisualElementRenderer.cs:244
at Xamarin.Forms.Platform.Android.VisualElementRenderer`1[TElement].Xamarin.Forms.Platform.Android.IVisualElementRenderer.SetElement (Xamarin.Forms.VisualElement element) [0x00033] in D:\a\1\s\Xamarin.Forms.Platform.Android\VisualElementRenderer.cs:131
at Xamarin.Forms.Platform.Android.Platform.CreateRenderer (Xamarin.Forms.VisualElement element, Android.Content.Context context) [0x0003e] in D:\a\1\s\Xamarin.Forms.Platform.Android\Platform.cs:358
at Xamarin.Forms.Platform.Android.VisualElementPackager.AddChild (Xamarin.Forms.VisualElement view, Xamarin.Forms.Platform.Android.IVisualElementRenderer oldRenderer, Xamarin.Forms.Platform.Android.RendererPool pool, System.Boolean sameChildren) [0x000b3] in D:\a\1\s\Xamarin.Forms.Platform.Android\VisualElementPackager.cs:140
at Xamarin.Forms.Platform.Android.VisualElementPackager.SetElement (Xamarin.Forms.VisualElement oldElement, Xamarin.Forms.VisualElement newElement) [0x00163] in D:\a\1\s\Xamarin.Forms.Platform.Android\VisualElementPackager.cs:351
at Xamarin.Forms.Platform.Android.VisualElementPackager.Load () [0x00000] in D:\a\1\s\Xamarin.Forms.Platform.Android\VisualElementPackager.cs:112
at Xamarin.Forms.Platform.Android.VisualElementRenderer`1[TElement].SetPackager (Xamarin.Forms.Platform.Android.VisualElementPackager packager) [0x00007] in D:\a\1\s\Xamarin.Forms.Platform.Android\VisualElementRenderer.cs:481
at Xamarin.Forms.Platform.Android.VisualElementRenderer`1[TElement].SetElement (TElement element) [0x000e8] in D:\a\1\s\Xamarin.Forms.Platform.Android\VisualElementRenderer.cs:244
at Xamarin.Forms.Platform.Android.VisualElementRenderer`1[TElement].Xamarin.Forms.Platform.Android.IVisualElementRenderer.SetElement (Xamarin.Forms.VisualElement element) [0x00033] in D:\a\1\s\Xamarin.Forms.Platform.Android\VisualElementRenderer.cs:131
at Xamarin.Forms.Platform.Android.Platform.CreateRenderer (Xamarin.Forms.VisualElement element, Android.Content.Context context) [0x0003e] in D:\a\1\s\Xamarin.Forms.Platform.Android\Platform.cs:358
at Xamarin.Forms.Platform.Android.VisualElementPackager.AddChild (Xamarin.Forms.VisualElement view, Xamarin.Forms.Platform.Android.IVisualElementRenderer oldRenderer, Xamarin.Forms.Platform.Android.RendererPool pool, System.Boolean sameChildren) [0x000b3] in D:\a\1\s\Xamarin.Forms.Platform.Android\VisualElementPackager.cs:140
at Xamarin.Forms.Platform.Android.VisualElementPackager.SetElement (Xamarin.Forms.VisualElement oldElement, Xamarin.Forms.VisualElement newElement) [0x00163] in D:\a\1\s\Xamarin.Forms.Platform.Android\VisualElementPackager.cs:351
at Xamarin.Forms.Platform.Android.VisualElementPackager.Load () [0x00000] in D:\a\1\s\Xamarin.Forms.Platform.Android\VisualElementPackager.cs:112
at Xamarin.Forms.Platform.Android.VisualElementRenderer`1[TElement].SetPackager (Xamarin.Forms.Platform.Android.VisualElementPackager packager) [0x00007] in D:\a\1\s\Xamarin.Forms.Platform.Android\VisualElementRenderer.cs:481
at Xamarin.Forms.Platform.Android.VisualElementRenderer`1[TElement].SetElement (TElement element) [0x000e8] in D:\a\1\s\Xamarin.Forms.Platform.Android\VisualElementRenderer.cs:244
at Xamarin.Forms.Platform.Android.VisualElementRenderer`1[TElement].Xamarin.Forms.Platform.Android.IVisualElementRenderer.SetElement (Xamarin.Forms.VisualElement element) [0x00033] in D:\a\1\s\Xamarin.Forms.Platform.Android\VisualElementRenderer.cs:131
at Xamarin.Forms.Platform.Android.Platform.CreateRenderer (Xamarin.Forms.VisualElement element, Android.Content.Context context) [0x0003e] in D:\a\1\s\Xamarin.Forms.Platform.Android\Platform.cs:358
at Xamarin.Forms.Platform.Android.VisualElementPackager.AddChild (Xamarin.Forms.VisualElement view, Xamarin.Forms.Platform.Android.IVisualElementRenderer oldRenderer, Xamarin.Forms.Platform.Android.RendererPool pool, System.Boolean sameChildren) [0x000b3] in D:\a\1\s\Xamarin.Forms.Platform.Android\VisualElementPackager.cs:140
at Xamarin.Forms.Platform.Android.VisualElementPackager.SetElement (Xamarin.Forms.VisualElement oldElement, Xamarin.Forms.VisualElement newElement) [0x00163] in D:\a\1\s\Xamarin.Forms.Platform.Android\VisualElementPackager.cs:351
at Xamarin.Forms.Platform.Android.VisualElementPackager.Load () [0x00000] in D:\a\1\s\Xamarin.Forms.Platform.Android\VisualElementPackager.cs:112
at Xamarin.Forms.Platform.Android.VisualElementRenderer`1[TElement].SetPackager (Xamarin.Forms.Platform.Android.VisualElementPackager packager) [0x00007] in D:\a\1\s\Xamarin.Forms.Platform.Android\VisualElementRenderer.cs:481
at Xamarin.Forms.Platform.Android.VisualElementRenderer`1[TElement].SetElement (TElement element) [0x000e8] in D:\a\1\s\Xamarin.Forms.Platform.Android\VisualElementRenderer.cs:244
at Xamarin.Forms.Platform.Android.VisualElementRenderer`1[TElement].Xamarin.Forms.Platform.Android.IVisualElementRenderer.SetElement (Xamarin.Forms.VisualElement element) [0x00033] in D:\a\1\s\Xamarin.Forms.Platform.Android\VisualElementRenderer.cs:131
at Xamarin.Forms.Platform.Android.Platform.CreateRenderer (Xamarin.Forms.VisualElement element, Android.Content.Context context) [0x0003e] in D:\a\1\s\Xamarin.Forms.Platform.Android\Platform.cs:358
at Xamarin.Forms.Platform.Android.VisualElementPackager.AddChild (Xamarin.Forms.VisualElement view, Xamarin.Forms.Platform.Android.IVisualElementRenderer oldRenderer, Xamarin.Forms.Platform.Android.RendererPool pool, System.Boolean sameChildren) [0x000b3] in D:\a\1\s\Xamarin.Forms.Platform.Android\VisualElementPackager.cs:140
at Xamarin.Forms.Platform.Android.VisualElementPackager.SetElement (Xamarin.Forms.VisualElement oldElement, Xamarin.Forms.VisualElement newElement) [0x00163] in D:\a\1\s\Xamarin.Forms.Platform.Android\VisualElementPackager.cs:351
at Xamarin.Forms.Platform.Android.VisualElementPackager.Load () [0x00000] in D:\a\1\s\Xamarin.Forms.Platform.Android\VisualElementPackager.cs:112
at Xamarin.Forms.Platform.Android.VisualElementRenderer`1[TElement].SetPackager (Xamarin.Forms.Platform.Android.VisualElementPackager packager) [0x00007] in D:\a\1\s\Xamarin.Forms.Platform.Android\VisualElementRenderer.cs:481
at Xamarin.Forms.Platform.Android.VisualElementRenderer`1[TElement].SetElement (TElement element) [0x000e8] in D:\a\1\s\Xamarin.Forms.Platform.Android\VisualElementRenderer.cs:244
at Xamarin.Forms.Platform.Android.VisualElementRenderer`1[TElement].Xamarin.Forms.Platform.Android.IVisualElementRenderer.SetElement (Xamarin.Forms.VisualElement element) [0x00033] in D:\a\1\s\Xamarin.Forms.Platform.Android\VisualElementRenderer.cs:131
at Xamarin.Forms.Platform.Android.Platform.CreateRenderer (Xamarin.Forms.VisualElement element, Android.Content.Context context) [0x0003e] in D:\a\1\s\Xamarin.Forms.Platform.Android\Platform.cs:358
at Xamarin.Forms.Platform.Android.AppCompat.Platform.AddChild (Xamarin.Forms.Page page, System.Boolean layout) [0x0000d] in D:\a\1\s\Xamarin.Forms.Platform.Android\AppCompat\Platform.cs:387
at Xamarin.Forms.Platform.Android.AppCompat.Platform.SetPageInternal (Xamarin.Forms.Page newRoot) [0x0005a] in D:\a\1\s\Xamarin.Forms.Platform.Android\AppCompat\Platform.cs:345
at Xamarin.Forms.Platform.Android.AppCompat.Platform.SetPage (Xamarin.Forms.Page newRoot) [0x000e6] in D:\a\1\s\Xamarin.Forms.Platform.Android\AppCompat\Platform.cs:299
at Xamarin.Forms.Platform.Android.FormsAppCompatActivity.InternalSetPage (Xamarin.Forms.Page page) [0x00039] in D:\a\1\s\Xamarin.Forms.Platform.Android\AppCompat\FormsAppCompatActivity.cs:508
at Xamarin.Forms.Platform.Android.FormsAppCompatActivity.SetMainPage () [0x00000] in D:\a\1\s\Xamarin.Forms.Platform.Android\AppCompat\FormsAppCompatActivity.cs:534
at Xamarin.Forms.Platform.Android.FormsAppCompatActivity.LoadApplication (Xamarin.Forms.Application application) [0x00140] in D:\a\1\s\Xamarin.Forms.Platform.Android\AppCompat\FormsAppCompatActivity.cs:193
... (Removed)
at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_savedInstanceState) [0x00010] in /Users/builder/azdo/_work/1/s/xamarin-android/src/Mono.Android/obj/Release/monoandroid10/android-31/mcw/Android.App.Activity.cs:2781
at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.7(intptr,intptr,intptr)
Do you have any suggestions or corrections?
Maybe I’m wrong about the implementation of CollectionView.
https://github.com/roubachof/Sharpnado.CollectionView#initialization
I also suggest you to use the TaskLoaderView instead of StateLayout.
It is specialized for view model loading.
I see in this moment the line "Initializer.Initilize" in the specified project.
I’m sorry I didn’t look.
I thought it was a line for another nuget, and I didn’t notice.
Thank you so much for the correction and the immediate response.