Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

NullReferenceException on Grid.CalculateAutoCells [Only XF iOS]

$
0
0

Hi,

I'm developing an app with Xamarin Forms from 7 months.
My app worked very well.
I have done 1 month on Android side, because I fixed some bugs and added some features.

Today I have ported these mods on iOS side and now I have a NullReferenceException on Grid, when I scroll the List View. (On Android all works perfectly)

Here's what happens:
- I open the page that contains the list
- The list is loaded and displayed correctly
- If I try to scroll through the list, appears this exception

Stack Trace:

at Xamarin.Forms.Grid.CalculateAutoCells (System.Double width, System.Double height) [0x00357] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Core\GridCalc.cs:144 at Xamarin.Forms.Grid.MeasureGrid (System.Double width, System.Double height, System.Boolean requestSize) [0x0000c] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Core\GridCalc.cs:483 at Xamarin.Forms.Grid.LayoutChildren (System.Double x, System.Double y, System.Double width, System.Double height) [0x0000e] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Core\GridCalc.cs:17 at Xamarin.Forms.Layout.UpdateChildrenLayout () [0x000c7] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Core\Layout.cs:229 at Xamarin.Forms.Layout.OnSizeAllocated (System.Double width, System.Double height) [0x0000f] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Core\Layout.cs:194 at Xamarin.Forms.VisualElement.SizeAllocated (System.Double width, System.Double height) [0x00000] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Core\VisualElement.cs:629 at Xamarin.Forms.Layout+<>c.<OnChildMeasureInvalidated>b__39_0 () [0x00080] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Core\Layout.cs:332 at Foundation.NSAsyncActionDispatcher.Apply () [0x00000] in /Users/builder/data/lanes/3969/44931ae8/source/xamarin-macios/src/Foundation/NSAction.cs:163 at (wrapper managed-to-native) UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr) at UIKit.UIApplication.Main (System.String[] args, System.IntPtr principal, System.IntPtr delegate) [0x00005] in /Users/builder/data/lanes/3969/44931ae8/source/xamarin-macios/src/UIKit/UIApplication.cs:79 at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x00038] in /Users/builder/data/lanes/3969/44931ae8/source/xamarin-macios/src/UIKit/UIApplication.cs:63 at MusiDownPortable.iOS.Application.Main (System.String[] args) [0x00008] in /Users/steebono/SourceTree/musidown/MusiDownPortable/MusiDownPortable/MusiDownPortable.iOS/Main.cs:17

Sometimes scroll works but when I try to reload list I have this exception:

Second StackTrace:

at Xamarin.Forms.Grid.GetLastAutoColumn (Xamarin.Forms.BindableObject child) [0x00016] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Core\GridCalc.cs:657 at Xamarin.Forms.Grid.ExpandLastAutoColumnIfNeeded (System.Double width, System.Boolean expandToRequest) [0x00024] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Core\GridCalc.cs:331 at Xamarin.Forms.Grid.MeasureGrid (System.Double width, System.Double height, System.Boolean requestSize) [0x0013a] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Core\GridCalc.cs:520 at Xamarin.Forms.Grid.LayoutChildren (System.Double x, System.Double y, System.Double width, System.Double height) [0x0000e] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Core\GridCalc.cs:17 at Xamarin.Forms.Layout.UpdateChildrenLayout () [0x000c7] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Core\Layout.cs:229 at Xamarin.Forms.Layout.OnSizeAllocated (System.Double width, System.Double height) [0x0000f] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Core\Layout.cs:194 at Xamarin.Forms.VisualElement.SizeAllocated (System.Double width, System.Double height) [0x00000] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Core\VisualElement.cs:629 at Xamarin.Forms.Layout+<>c.<OnChildMeasureInvalidated>b__39_0 () [0x00080] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Core\Layout.cs:332 at Foundation.NSAsyncActionDispatcher.Apply () [0x00000] in /Users/builder/data/lanes/3969/7beaef43/source/xamarin-macios/src/Foundation/NSAction.cs:163 at (wrapper managed-to-native) UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr) at UIKit.UIApplication.Main (System.String[] args, System.IntPtr principal, System.IntPtr delegate) [0x00005] in /Users/builder/data/lanes/3969/7beaef43/source/xamarin-macios/src/UIKit/UIApplication.cs:79 at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x00038] in /Users/builder/data/lanes/3969/7beaef43/source/xamarin-macios/src/UIKit/UIApplication.cs:63 at MusiDownPortable.iOS.Application.Main (System.String[] args) [0x00008] in /Users/steebono/SourceTree/musidown/MusiDownPortable/MusiDownPortable/MusiDownPortable.iOS/Main.cs:17

XAML Code:

<ListView x:Name="listView" ItemSelected="OnItemSelected" IsVisible="False" HasUnevenRows="False" RowHeight="50" IsPullToRefreshEnabled="True" Refreshing="mySongs_OnRefreshing"> <x:Arguments> <ListViewCachingStrategy>RecycleElement</ListViewCachingStrategy> </x:Arguments> <ListView.ItemTemplate> <DataTemplate> <ViewCell> <ViewCell.ContextActions> <MenuItem Clicked="mySongDelete_OnClicked" CommandParameter="{Binding .}" Text="Delete" IsDestructive="True" /> </ViewCell.ContextActions> <Grid> <Grid.RowDefinitions> <RowDefinition Height="50" /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="5" /> <ColumnDefinition Width="35" /> <ColumnDefinition Width="*" /> <ColumnDefinition Width="35" /> <ColumnDefinition Width="5" /> </Grid.ColumnDefinitions> <ff:CachedImage Source="{Binding FavoriteIcon}" CacheDuration="1" RetryCount="1" Aspect="AspectFit" HorizontalOptions="StartAndExpand" Grid.Row="0" Grid.Column="1"> <ff:CachedImage.GestureRecognizers> <TapGestureRecognizer Tapped="onClick_Favorite"/> </ff:CachedImage.GestureRecognizers> </ff:CachedImage> <Label Text="{Binding Title}" VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand" VerticalTextAlignment="Center" HorizontalTextAlignment="Center" LineBreakMode="TailTruncation" FontSize="Medium" Grid.Row="0" Grid.Column="2" /> <ff:CachedImage Source="{Binding MenuIcon}" CacheDuration="1" RetryCount="1" Aspect="AspectFit" HorizontalOptions="EndAndExpand" Grid.Row="0" Grid.Column="3"> <ff:CachedImage.GestureRecognizers> <TapGestureRecognizer Tapped="onClick_Menu"/> </ff:CachedImage.GestureRecognizers> </ff:CachedImage> </Grid> </ViewCell> </DataTemplate> </ListView.ItemTemplate> </ListView>

C# Code:

Inside OnAppearing():

`

// Other code here

listView.ItemSource = _myListItems;

// Other code here

`

My List is declared as:

`

private ObservableCollection<MyItemEntry> _myListItems = new ObservableCollection<MyItemEntry>();

`

MyItemEntry object is a class derived from INotifyPropertyChanged

I have an event that load and reload the list if needed:

`

        // Other code here

        // Start async operation
                Task.Run(() =>
                {
                    // Clear List
                    Device.BeginInvokeOnMainThread(() =>
                    {
                        if (_myListItems.Any()) _myListItems.Clear();
                    });
                    // Get refreshed list.
                    var myList = DependencyService.Get<IItemHandler>().ReadItem;

                    if (myList.Any())
                    {
                        Device.BeginInvokeOnMainThread(() =>
                        {
                            foreach (var itemDataEntry in myList)
                            {
                var menuIcon = "Menu100.png";
                                var favoriteIcon = itemDataEntry.Favorite ? "Favfill.png" : "Fav100.png";
                                _myListItems.Add(new MyItemEntry
                                {
                                    FavoriteIcon = favoriteIcon,
                                    MenuIcon = menuIcon,
                                    Title = decSong
                                });
                            }
                            if (_mySongsItems.Any())
                            {
                                Device.BeginInvokeOnMainThread(() =>
                                {
                                    listView.IsVisible = true;
                                    loadingIndicator.IsVisible = false;
                                    loadingIndicator.IsRunning = false;
                                    layoutDataInfo.IsVisible = false;
                                    lbDataInfo.IsVisible = false;
                                });
                            }
                            else
                            {
                                Device.BeginInvokeOnMainThread(() =>
                                {
                                    listView.IsVisible = false;
                                    loadingIndicator.IsVisible = false;
                                    loadingIndicator.IsRunning = false;
                                    layoutDataInfo.IsVisible = true;
                                    imgDataInfo.IsVisible = true;
                                    lbDataInfo.IsVisible = true;
                                });
                            }
                        });
                    }
                    else
                    {
                        Device.BeginInvokeOnMainThread(() =>
                        {
                            listView.IsVisible = false;
                            loadingIndicator.IsVisible = false;
                            loadingIndicator.IsRunning = false;
                            layoutDataInfo.IsVisible = true;
                            imgDataInfo.IsVisible = true;
                            lbDataInfo.IsVisible = true;
                        });
                    }
                });

// Other code here for update list if needed

`

This codes on Android work perfectly. On iOS this codes work perfectly but if i try to scroll the list, I have that Exception.

A important thing is that this Event do not fire when I scroll the List.
I think that my code is not the cause of this Exception, because this happend only if I scroll
If I open the page and after do nothing, all work perfectly. I do not manage in any way scrolling or similar.

I have viewed the code of Xamarin Libraries and seems That Grid.ColumnDefinitions at some point became null.

First StackTrace say:

at Xamarin.Forms.Grid.CalculateAutoCells (System.Double width, System.Double height) [0x00357] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Core\GridCalc.cs:144

In this line there is a for cycle:

for (var i = 0; i < _columns.Count; i++)

Second StackTrace say:

at Xamarin.Forms.Grid.CalculateAutoCells (System.Double width, System.Double height) [0x00357] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Core\GridCalc.cs:657

In this line there is a if with same variable of first stacktrace:
if (_columns[i].Width.IsAuto)

so I think that the null reference exception is caused by _columns var.
As you can see in my xaml code I have set ColumnDefinitions.

This error happens when the list has already been created and displayed so.. someone set this to null at some point or Layout.UpdateChildrenLayout fails to get ColumnDefinitions value?

Another observation is that I set ItemSource of listView inside OnAppearing method; when I reload page, I clear my list object and I re-insert all updated items to list. But I think that this is not a problem because my list's items derived from INotifyPropertyChanged and my list is a ObservableCollecion list.

Xamarin Mac Env: last version iOS: 10.1 Xamarin Forms: 2.3.3.168 / 2.3.3.180 (Tried both)

Sorry for my english.

Thanks,
Stefano


Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>