What's up with this new System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

My app is always up-to-date with the latest release.

From about a month ago, I get a random crash that gets reported to insights as shown below. Any pointers appreciated.

Java.Lang.RuntimeExceptionlength=20; index=20
Raw
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in :0
at Android.Runtime.JNIEnv.CallVoidMethod (IntPtr jobject, IntPtr jmethod, Android.Runtime.JValue* parms) [0x00069] in :0
at Android.Views.ViewGroup.Layout (Int32 l, Int32 t, Int32 r, Int32 b) [0x00088] in :0
at Xamarin.Forms.Platform.Android.ViewRenderer`2[TView,TNativeView].OnLayout (Boolean changed, Int32 l, Int32 t, Int32 r, Int32 b) [0x00059] in :0
at Xamarin.Forms.Platform.Android.ListViewRenderer.OnLayout (Boolean changed, Int32 l, Int32 t, Int32 r, Int32 b) [0x00000] in :0
at Xamarin.Forms.Platform.Android.FormsViewGroup.n_OnLayout_ZIIII (IntPtr jnienv, IntPtr native__this, Boolean p0, Int32 p1, Int32 p2, Int32 p3, Int32 p4) [0x00008] in :0
at (wrapper dynamic-method) System.Object:df2d3944-9866-43fb-8491-10b7c12faae9 (intptr,intptr,bool,int,int,int,int)
--- End of managed exception stack trace ---
java.lang.ArrayIndexOutOfBoundsException: length=20; index=20
at android.widget.AbsListView$RecycleBin.addScrapView(AbsListView.java:6437)
at android.widget.ListView.layoutChildren(ListView.java:1559)
at android.widget.AbsListView.onLayout(AbsListView.java:2102)
at android.view.View.layout(View.java:13772)
at android.view.ViewGroup.layout(ViewGroup.java:4365)
at android.support.v4.widget.SwipeRefreshLayout.onLayout(SwipeRefreshLayout.java:581)
at android.view.View.layout(View.java:13772)
at android.view.ViewGroup.layout(ViewGroup.java:4365)
at md5b60ffeb829f638581ab2bb9b1a7f4f3f.ListViewRenderer.n_onLayout(Native Method)
at md5b60ffeb829f638581ab2bb9b1a7f4f3f.ListViewRenderer.onLayout(ListViewRenderer.java:64)
at android.view.View.layout(View.java:13772)
at android.view.ViewGroup.layout(ViewGroup.java:4365)
at com.xamarin.forms.platform.android.FormsViewGroup.measureAndLayout(FormsViewGroup.java:29)
at md5b60ffeb829f638581ab2bb9b1a7f4f3f.VisualElementRenderer_1.n_onLayout(Native Method)
at md5b60ffeb829f638581ab2bb9b1a7f4f3f.VisualElementRenderer_1.onLayout(VisualElementRenderer_1.java:48)
at android.view.View.layout(View.java:13772)
at android.view.ViewGroup.layout(ViewGroup.java:4365)
at com.xamarin.forms.platform.android.FormsViewGroup.measureAndLayout(FormsViewGroup.java:29)
at md5b60ffeb829f638581ab2bb9b1a7f4f3f.VisualElementRenderer_1.n_onLayout(Native Method)
at md5b60ffeb829f638581ab2bb9b1a7f4f3f.VisualElementRenderer_1.onLayout(VisualElementRenderer_1.java:48)
at android.view.View.layout(View.java:13772)
at android.view.ViewGroup.layout(ViewGroup.java:4365)
at com.xamarin.forms.platform.android.FormsViewGroup.measureAndLayout(FormsViewGroup.java:29)
at md5b60ffeb829f638581ab2bb9b1a7f4f3f.NavigationRenderer.n_onLayout(Native Method)
at md5b60ffeb829f638581ab2bb9b1a7f4f3f.NavigationRenderer.onLayout(NavigationRenderer.java:46)
at android.view.View.layout(View.java:13772)
at android.view.ViewGroup.layout(ViewGroup.java:4365)
at com.xamarin.forms.platform.android.FormsViewGroup.measureAndLayout(FormsViewGroup.java:29)
at md5b60ffeb829f638581ab2bb9b1a7f4f3f.PlatformRenderer.n_onLayout(Native Method)
at md5b60ffeb829f638581ab2bb9b1a7f4f3f.PlatformRenderer.onLayout(PlatformRenderer.java:54)
at android.view.View.layout(View.java:13772)
at android.view.ViewGroup.layout(ViewGroup.java:4365)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649)
at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1638)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1422)
at android.view.View.layout(View.java:13772)
at android.view.ViewGroup.layout(ViewGroup.java:4365)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:13772)
at android.view.ViewGroup.layout(ViewGroup.java:4365)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1507)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1420)
at android.view.View.layout(View.java:13772)
at android.view.ViewGroup.layout(ViewGroup.java:4365)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:13772)
at android.view.ViewGroup.layout(ViewGroup.java:4365)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1874)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1695)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1006)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4220)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
at android.view.Choreographer.doCallbacks(Choreographer.java:555)
at android.view.Choreographer.doFrame(Choreographer.java:525)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)

Answers

  • Exact same problem.
    FrancMorales, do you resolved this issue?

  • FrancMoralesFrancMorales JPMember ✭✭

    No.

    The only workaround I've found is to revert to Xamarin.Forms version 2.0.1.6505, anything newer produces this crash.

  • JohnHardmanJohnHardman GBUniversity mod
    edited May 2016

    @FrancMorales @Mikalai I have the same issue when scrolling a ListView using XF 2.2.0.31 . Has this been reported to Xamarin support?

    [Edit - I've just logged it in Bugzilla at https://bugzilla.xamarin.com/show_bug.cgi?id=41118 ]

  • FrancMoralesFrancMorales JPMember ✭✭

    @JohnHardman

    That's the one. ListView crashes, at times on scroll, at times when the form opens. It is an inconsistent crash that occurs once in a while. I didn't open a bug report because I cannot create a reliable reproducible scenario and, as expected, they often require one.

    Also, I have not found anyone else reporting the issue. If you guys have, please post it here.

  • JohnHardmanJohnHardman GBUniversity mod

    @FrancMorales @Mikalai - Are you using a DataTemplateSelector ? Just wondering if that might be the common factor between people hitting this problem?

  • FrancMoralesFrancMorales JPMember ✭✭

    No. But the listview that gives trouble uses complex ItemTemplates that override OnBindingContextChanged() to arrange controls within each listview item on the fly.

  • FrancMoralesFrancMorales JPMember ✭✭

    @JohnHardman

    Have you tried the latest update 2.3.0.49?

    My tests so far are good and the bug seems to be gone but I'm reluctant to push it to production just yet.

    See if it works for you too. You seem to be coming at the problem from a different perspective.

  • JohnHardmanJohnHardman GBUniversity mod

    @FrancMorales - No, I'm on 2.2.0.45 currently. However, I no longer hit that problem - a change I made to my own code resulted in me no longer hitting the problem. Whether the execution path within the XF code changed or whether a change in timing avoided a race condition, I do not know.

  • FrancMoralesFrancMorales JPMember ✭✭

    Thanks for the heads up.

  • FrancMoralesFrancMorales JPMember ✭✭

    Nope. The crash remains.

  • rmarinhormarinho PTMember, Insider, Beta Xamurai

    Hi i'm not able to reproduce this error. Please if you can provide a reproduction case on bug 41118.

    Thanks!

  • FrancMoralesFrancMorales JPMember ✭✭

    @Rui Marinho

    Thanks for following up on this. I'll see if I can create a simple project that isolates the problem. At present, it is deep within a relatively complex app.

  • @JohnHardman On June 17th you mentioned you changed the code to not hit the problem - what was your fix for this? I'm hitting similar problem as initially reported by @FrankMorales , RecycleBin.addScrapView with IndexOutOfBounds exception, and we use DataTemplateSelector for list content. This is with Forms v2.3.0.107.

    Looking on the BugZilla issue (https://bugzilla.xamarin.com/show_bug.cgi?id=41118 ) it doesn't look like they could reproduce the issue without an example so hasn't been fixed on v2.3.2.127 either :-(

  • JohnHardmanJohnHardman GBUniversity mod

    @StephenClifton.9427 - re. "you mentioned you changed the code to not hit the problem". I didn't actually change the code to not hit the problem, more a change that I made to the code unwittingly made the problem go away. Not ideal, but it hasn't come back (so far). Unfortunately, I don't know what the change was that made it go away.

    If you have code that reproduces this now, can you attach it to the bug in Bugzilla pls so that Xamarin can hopefully pinpoint the cause.

  • MithileshNaikMithileshNaik USMember ✭✭

    Having the same issue. A common factor here is DataTemplateSelector.

    Did you find any resolution yet ?

  • FrancMoralesFrancMorales JPMember ✭✭

    ItemTemplate can only be changed 20 times. After that, the system burps the exception at the top of this thread.

    It is a limitation that I don't understand but that is the problem. Find alternative and that will be your resolution.

    In my case, I used ItemTemplate to refresh the list. I no longer do and the exception went away.

    Hope it helps.

  • CiaranMCiaranM IEMember
    edited March 2017

    +1 with the same issue here. Tried using data Templates instead then after; but then I could not get the list view to update itself.

  • FrancMoralesFrancMorales JPMember ✭✭

    I could be wrong, but I seem to remember that a hack workaround to refresh is to set ItemSource to null and then back to whatever data source.

    The proper way is to use bindings but this gets tricky with complex cells and data structures as each element must be bound. There is also a performance penalty if there are lots of items.

  • CiaranMCiaranM IEMember
    edited March 2017

    I don't know if this will help anyone but I believe I may have solved my own problem with the ListView being able to have only 20 ItemTemplates.

    I had my Items in a wrapper

    ```c#
    public class SelectibleItemWrapper
    {
    public bool isSelected { get; set; }
    public T Item { get; set; }
    public int Index { get; set; }

    }


    With a selector

    ```c#
    class ItemDataTemplateSelector : DataTemplateSelector
    {
    public DataTemplate ValidTemplate { get; set; }
    public DataTemplate InvalidTemplate { get; set; }

        protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
        {
            var wrappedItem = (SelectibleItemWrapper<Item>)item;
    
            if (wrappedItem == null)
                return null;
    
            if(wrappedItem.isSelected)
            {
                return ValidTemplate;
            }
            else
            {
                return InvalidTemplate;
            }
        }
    }
    

    Then finally when the items in the source have changed (One was selected) I re-set the ItemsSource, removed the Selector and then set the selector again.

    ```c#
    private void ListView_ItemTapped(object sender, ItemTappedEventArgs e)
    {
    var element = (SelectibleItemWrapper)e.Item;

            if (element.Index < itemList.Count && element.Index >= 0)
            {
                if (element.isSelected)
                {
                    this.itemList[element.Index].isSelected = false;
                }
                else
                {
                    this.itemList[element.Index].isSelected = true;
                }
            }
    
            this.listView.ClearValue(ListView.ItemTemplateProperty);
            this.listView.ItemsSource = this.itemList;
    
            this.listView.ItemTemplate = new ItemDataTemplateSelector
            {
                ValidTemplate = validTemplate,
                InvalidTemplate = invalidTemplate
            };
    
        }
    

    Took me a while to get this so I though I may as well share in case it helps someone else.

  • CiaranMCiaranM IEMember

    I believed I may have finally fixed my problem. I dont know how efficient/inefficient it is but I have to share in case I help someone else. I have seen similar ways but I found one I had problems with those too.

    Just to say this is a ListView with multiple selection and data templates without getting the error for having over 20 ItemTemplates.

    I used an Item Wrapper containing a boolean for selection

    public class SelectibleItemWrapper<T>
        {
            public bool isSelected { get; set; }
            public T Item { get; set; }
            public int Index { get; set; }
    
        }
    

    With a data template selector to choose when something was selected

    class ItemDataTemplateSelector : DataTemplateSelector
        {
            public DataTemplate ValidTemplate { get; set; }
            public DataTemplate InvalidTemplate { get; set; }
    
            protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
            {
                var wrappedItem = (SelectibleItemWrapper<Item>)item;
    
                if (wrappedItem == null)
                    return null;
    
                if(wrappedItem.isSelected)
                {
                    return ValidTemplate;
                }
                else
                {
                    return InvalidTemplate;
                }
            }
        }
    

    Just so there is little confusion here is my form that contains the list and starting code, the ListViewCachingStrategy stops another issue.

     public class UploadItemForm : ContentPage
        {
    
            private ListView listView = new ListView(ListViewCachingStrategy.RecycleElement);
            private List<SelectibleItemWrapper<Item>> itemList = new List<SelectibleItemWrapper<Item>>();
    
            DataTemplate validTemplate;
            DataTemplate invalidTemplate;
    
            StackLayout contentStack;
    
            public UploadItemForm()
            {
    
                Title = Resource.SelectItemsForUpload;
                BindingContext = this;
    
                PopulateList();
                SetupDataTemplates();
    
                var uploadButton = new Button
                {
                    Text = Resource.UploadSelectedItems
                };
    
                this.listView.ItemTapped += ListView_ItemTapped;
                uploadButton.Clicked += UploadItems;
    
    
                this.contentStack = new StackLayout()
                {
                    Children =
                    {
                        this.listView,
                        uploadButton
                    }
                };
    
                Content = contentStack;
            }
    

    Then having my data templates set

    #region List Data Templates
    
    void SetupDataTemplates()
            {
                validTemplate = new DataTemplate(() => 
                {
                    var nameLabel = new Label { FontAttributes = FontAttributes.None };
                    nameLabel.SetBinding(Label.TextProperty, "Item.Name");
                    nameLabel.Margin = new Thickness(20, 5, 0, 0);
                    nameLabel.TextColor = Color.Black;
    
                    var grid = new Grid();
                    grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(0.8, GridUnitType.Star) });
                    grid.Children.Add(nameLabel);
                    grid.BackgroundColor = Color.FromRgb(170, 170, 170);
    
                    return new ViewCell
                    {
                        View = grid
                    };
                });
    
                invalidTemplate = new DataTemplate(() => 
                {
                    var nameLabel = new Label { FontAttributes = FontAttributes.None };
                    nameLabel.SetBinding(Label.TextProperty, "Item.Name");
                    nameLabel.Margin = new Thickness(20, 5, 0, 0);
                    nameLabel.TextColor = Color.Black;
    
                    var grid = new Grid();
                    grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(0.8, GridUnitType.Star) });
                    grid.Children.Add(nameLabel);
                    grid.BackgroundColor = Color.FromRgb(255, 255, 255);
    
                    return new ViewCell
                    {
                        View = grid
                    };
                });
            }
    
    #endregion
    
    

    setting my source items and the itemSelector. The async can be removed, it was just for my item source

        private async void PopulateList()
            {
                this.itemList = new List<SelectibleItemWrapper<Item>>();
    
                List<Item> items = await App.DataRepository.GetItemsForUpload();
    
                int count = 0;
                foreach (Item i in items)
                {
                    this.itemList.Add(new SelectibleItemWrapper<Item>() { Item = i, isSelected = false, Index = count });
                    count++;
                }
    
                if(listView.GetValue(ListView.ItemTemplateProperty) != null)
                    this.listView.ClearValue(ListView.ItemTemplateProperty);
    
                this.listView.ItemsSource = this.itemList;
    
                this.listView.ItemTemplate = new ItemDataTemplateSelector
                {
                    ValidTemplate = validTemplate,
                    InvalidTemplate = invalidTemplate
                };
            }
    

    and finally when an item is selected in the list, change it in the wrapper, and clear the previous selector, set the ItemsSource again, and set the Item Template to the selector again.

        private void ListView_ItemTapped(object sender, ItemTappedEventArgs e)
            {
                var element = (SelectibleItemWrapper<Item>)e.Item;
    
                if (element.Index < itemList.Count && element.Index >= 0)
                {
                    if (element.isSelected)
                    {
                        this.itemList[element.Index].isSelected = false;
                    }
                    else
                    {
                        this.itemList[element.Index].isSelected = true;
                    }
                }
    
                this.listView.ClearValue(ListView.ItemTemplateProperty);
                this.listView.ItemsSource = this.itemList;
    
                this.listView.ItemTemplate = new ItemDataTemplateSelector
                {
                    ValidTemplate = validTemplate,
                    InvalidTemplate = invalidTemplate
                };
            }
    

    I know this is not efficient. There is a lot of re-drawing, refreshing rather than updating. This is the only way I was able to get this working myself. I will try again another time but for now this way will do for me.

    I hope this helps someone :smile:

Sign In or Register to comment.