Forum Xamarin.Forms

Crash when scroll ListView on Android


I careated ListView with different types of cells

        public SettingsView () { //Constructor

            rows = new List<object>();//this is data for ListView

            listView = this.FindByName<ListView>("list_view");
            listView.ItemTemplate = new SettingsTemplateSelector();

Here is SettingsTemplateSelector class

    public class SettingsTemplateSelector : DataTemplateSelector {
        public double width = 0;

        protected override DataTemplate OnSelectTemplate(object item, BindableObject container) {
            if (item is int) {
                return new DataTemplate(() => {
                    Cells.SeparatorCell cell = new Cells.SeparatorCell();
                    cell.cellWidth = width;
                    cell.type = (int)item;
                    return cell;

            if (item is MTitle) {
                return new DataTemplate(() => {
                    //double w = ((ListView)container).WidthRequest;
                    Cells.TitleCell cell = new Cells.TitleCell();
                    cell.cellWidth = width;
                    cell.item = (MTitle)item;

                    return cell;

            return null;

I also have this function to update ListView

    public void reloadData (double width = 0) {
        listView.ItemsSource = null;
        ((SettingsTemplateSelector)listView.ItemTemplate).width = width;
        listView.ItemsSource = rows;


When I run the program and trying scroll my listview I receive crash:

Unhandled Exception:
Java.Lang.ArrayIndexOutOfBoundsException: occurred

For iOS all works perfect but for Android crash happens. Anybody collided with this?


  • I've encountered the same problem.

    The problem is probable that you create a new instance of DataTemplate each time. Once you created a template for 'object item' in OnSelectTemplate you should always return the same instance. With other words cache your templates.
    See in Documentation-> The DataTemplateSelector must not return new instances of a DataTemplate on each call, instead the same instance must be returned. Failure to do so will effectively disable virtualization and lead to an enormous memory leak. Don't do this

  • aleks1aleks1 Member ✭✭

    StephanKurmann, yes .. you are rigth ... But how in this case I can send data to each cell form my array list?
    I need to do this before call OnBindingContextChanged in my cell.

  • aleks1aleks1 Member ✭✭

    I got it. In the OnBindingContextChanged I can call BindingContext property. It will be my data from array list.

Sign In or Register to comment.