Bug 21913 - [Android] ImageView 'jobject' must not be IntPtr.Zero.
Summary: [Android] ImageView 'jobject' must not be IntPtr.Zero.
Status: RESOLVED FIXED
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 1.2.2
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2014-08-07 11:41 UTC by Chase Florell
Modified: 2014-08-12 15:44 UTC (History)
4 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
the image used for the test (6.71 KB, image/png)
2014-08-07 11:41 UTC, Chase Florell
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Chase Florell 2014-08-07 11:41:29 UTC
Created attachment 7621 [details]
the image used for the test

I'm not sure if this is a duplicate of this thread
https://bugzilla.xamarin.com/show_bug.cgi?id=21618

But in general, images in the ListView seem to quickly crash the app.

Steps to reproduce

1. paste the code below into a fresh App.cs
2. copy the image attached into your PCL Resources directory and set it's Build Action to Embedded
3. Run the app, and then navigate around, clicking into the listview, swiping between Carousel pages, and even going back. It won't take long for the app to crash.

--------------------------------------------------------

The Exception

08-07 09:34:32.984 E/mono    ( 5743): Unhandled Exception:
08-07 09:34:32.984 E/mono    ( 5743): System.ArgumentException: 'jobject' must not be IntPtr.Zero.
08-07 09:34:32.984 E/mono    ( 5743): Parameter name: jobject
08-07 09:34:32.984 E/mono    ( 5743):   at Android.Runtime.JNIEnv.CallObjectMethod (IntPtr jobject, IntPtr jmethod) [0x00010] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.14-series/a5d57087/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:169 
08-07 09:34:32.984 E/mono    ( 5743):   at Android.Widget.ImageView.get_Drawable () [0x00043] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.14-series/a5d57087/source/monodroid/src/Mono.Android/platforms/android-19/src/generated/Android.Widget.ImageView.cs:511 
08-07 09:34:32.984 E/mono    ( 5743):   at Xamarin.Forms.Platform.Android.ImageRenderer.Dispose (Boolean disposing) [0x00000] in <filename unknown>:0 
08-07 09:34:32.984 E/mono    ( 5743):   at Java.Lang.Object.Finalize () [0x00056] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.14-series/a5d57087/source/monodroid/src/Mono.Android/src/Java.Lang/Object.cs:61 
The program 'Mono' has exited with code 0 (0x0).

--------------------------------------------------------

The Code (complete)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace perfomance
{
    public class App
    {
        public static INavigation Nav;

        public static Page GetMainPage()
        {
            var root = new MyRootPage();
            var navWrapper = new NavigationPage(root);
            Nav = navWrapper.Navigation;
            return navWrapper;
        }
    }

    public class MyRootPage : ContentPage
    {
        private readonly Button _button;

        public MyRootPage()
        {
            _button = new Button {Text = "Click me!", HorizontalOptions = LayoutOptions.FillAndExpand, VerticalOptions = LayoutOptions.StartAndExpand};
            Content = _button;
        }

        protected override void OnAppearing()
        {
            base.OnAppearing();
            _button.Clicked += async (s, e) =>
            {
                var cp = new MyCarouselPage();
                await App.Nav.PushAsync(cp);
            };
        }
    }

    public class MyCarouselPage : CarouselPage
    {
        public MyCarouselPage()
        {
            var page1 = CreateACarouselSection();
            Children.Add(page1);

            var page2 = CreateACarouselSection();
            Children.Add(page2);
        }

        private static MyCarouselSection CreateACarouselSection()
        {
            var dvm1 = new DataViewModel();
            return new MyCarouselSection(dvm1);
        }
    }

    public class MyCarouselSection : ContentPage
    {
        private readonly DataViewModel _dataViewModel;

        public MyCarouselSection(DataViewModel dataViewModel)
        {
            _dataViewModel = dataViewModel;
            BindingContext = _dataViewModel;

            var dataTemplate = new DataTemplate(typeof (MyListCellTemplate));
            dataTemplate.CreateContent();

            var list = new ListView
            {
                HorizontalOptions = LayoutOptions.FillAndExpand,
                VerticalOptions = LayoutOptions.FillAndExpand,
                ItemTemplate = dataTemplate
            };
            var header = new Label {Text = "Header", VerticalOptions = LayoutOptions.Start, HorizontalOptions = LayoutOptions.FillAndExpand};
            var footer = new Label {Text = "Footer", VerticalOptions = LayoutOptions.End, HorizontalOptions = LayoutOptions.FillAndExpand};

            list.SetBinding(ListView.ItemsSourceProperty, "Data");
            Content = new StackLayout {Orientation = StackOrientation.Vertical, Padding = 10, Children = {header, list, footer}};
        }

        protected override async void OnAppearing()
        {
            base.OnAppearing();

            for (var i = 0; i < 50; i++)
            {
                // awaiting this allows the list item to build one at a time == WIN!
                await Task.Run(() => _dataViewModel.Data.Add(new Data()));
            }
        }
    }

    public class DataViewModel
    {
        private IList<Data> _data;

        public DataViewModel()
        {
            Data = new List<Data>();
        }

        public IList<Data> Data
        {
            get { return _data; }
            set
            {
                _data = value;
                OnPropertyChanged();
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            var handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public class MyListCellTemplate : ViewCell
    {
        public MyListCellTemplate()
        {
            var ico = new Image {HorizontalOptions = LayoutOptions.Start, VerticalOptions = LayoutOptions.FillAndExpand};

            var val1 = new Label
            {
                Font = Font.SystemFontOfSize(NamedSize.Small, FontAttributes.Bold | FontAttributes.Italic),
                VerticalOptions = LayoutOptions.FillAndExpand,
                HorizontalOptions = LayoutOptions.FillAndExpand
            };

            var val2 = new Label
            {
                Font = Font.SystemFontOfSize(NamedSize.Micro),
                VerticalOptions = LayoutOptions.FillAndExpand,
                HorizontalOptions = LayoutOptions.FillAndExpand
            };
            var val3 = new Label {HorizontalOptions = LayoutOptions.End, VerticalOptions = LayoutOptions.FillAndExpand};

            ico.SetBinding(Image.SourceProperty, "Ico", BindingMode.OneWay, new IconConverter());
            val1.SetBinding(Label.TextProperty, "Val1", BindingMode.OneWay);
            val2.SetBinding(Label.TextProperty, "Val2", BindingMode.OneWay);
            val3.SetBinding(Label.TextProperty, "Val3", BindingMode.OneWay);

            var innerLayout = new StackLayout
            {
                Orientation = StackOrientation.Vertical,
                HorizontalOptions = LayoutOptions.FillAndExpand,
                Children =
                {
                    val1,
                    val2
                }
            };

            View = new StackLayout
            {
                Orientation = StackOrientation.Horizontal,
                Children =
                {
                    ico,
                    innerLayout,
                    val3
                }
            };
        }
    }

    public class IconConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return ImageSource.FromResource(value.ToString());
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }
    }

    public class Data
    {
        public Data()
        {
            Ico = "perfomance.Resources.img.png";
            Val1 = "Value 1";
            Val2 = "Value 2";
            Val3 = "100";
        }

        public string Ico { get; set; }
        public string Val1 { get; set; }
        public string Val2 { get; set; }
        public string Val3 { get; set; }
    }
}
Comment 1 John Miller [MSFT] 2014-08-07 23:03:38 UTC
I am also seeing this when having a ListView where the ViewCells have an Image with a binding to an image from a URL. 

>[mono] Unhandled Exception:
>[mono] System.ArgumentException: 'jobject' must not be IntPtr.Zero.
>[mono] Parameter name: jobject
>[mono]   at Android.Runtime.JNIEnv.CallObjectMethod (IntPtr jobject, IntPtr jmethod) [0x00010] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.12-series/163212a9/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:169 
>[mono]   at Android.Widget.ImageView.get_Drawable () [0x00043] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.12-series/163212a9/source/monodroid/src/Mono.Android/platforms/android-19/src/generated/Android.Widget.ImageView.cs:511 
>[mono]   at Xamarin.Forms.Platform.Android.ImageRenderer.Dispose (Boolean disposing) [0x00000] in <filename unknown>:0 
>[mono]   at Java.Lang.Object.Finalize () [0x00056] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.12-series/163212a9/source/monodroid/src/Mono.Android/src/Java.Lang/Object.cs:61
Comment 3 Eric Maupin 2014-08-12 15:44:52 UTC
Fixed for next update.