NAXAM / mapbox-xamarin-forms

Mapbox on Xamarin.Forms
Apache License 2.0
73 stars 41 forks source link

Xamarin.Forms[Android] app crashing with Annotations #88

Open andreybel opened 4 years ago

andreybel commented 4 years ago

Hi! I'm trying to implement Mapbox in my Xamarin.Forms app and have a problem. When i'm adding List of Annotations to Map and restart app, i'm getting Exception:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.graphics.Bitmap$Config android.graphics.Bitmap.getConfig()' on a null object reference

My Annotation: _Annotations.Add(new SymbolAnnotation { Coordinates = new LatLng(50.4308233, 30.468171), Id = Guid.NewGuid().ToString(), Title = "test point", SubTitle = "test point description", IconImage = (ImageSource)"icpin8.png", // problem is here IsDraggable = false });

NOTE: It doesn't happen during first start, only after OnSleep() - OnResume(). Tell me please, where is my mistake? Thank's!

P.S. problem detected on Android

tuyen-vuduc commented 4 years ago

Hy @andreybel ,

Could you share a code sample for the issue?

Cheers.

andreybel commented 4 years ago

Here is my code sample. I'm using Prism MVVM in my project.

**MapPage.xaml**

 <mapbox:MapView
                x:Name="map" 
                      Grid.Row="0"
                      Grid.RowSpan="5"
                      ShowUserLocation="True"
                      DidFinishLoadingStyleCommand="{Binding DidFinishLoadingStyleCommand}"
                      DidTapOnMarkerCommand="{Binding DidTapOnMarkerCommand}"
                      VerticalOptions="FillAndExpand"
                      HorizontalOptions="Fill"/>

**MapPage.xaml.cs**

public partial class MapPage : ContentPage
{
        public MapPage()
        {
            InitializeComponent();
            map.Center = new LatLng(50.4308233, 30.468171);
            map.ZoomLevel = 12;
            map.Pitch = 60;
            map.MapStyle = MapStyle.STREETS;

        }
}

**MapPageViewModel.cs**

private ObservableCollection<Annotation> _annotations;
        public ObservableCollection<Annotation> Annotations
        {
            get => _annotations;
            set => SetProperty(ref _annotations, value);
        }

private DelegateCommand<MapStyle> _didFinishLoadingStyleCommand;
        public DelegateCommand<MapStyle> DidFinishLoadingStyleCommand => (_didFinishLoadingStyleCommand 
            ?? (_didFinishLoadingStyleCommand = new DelegateCommand<MapStyle>(OnDidFinishLoadingStyle)));

        private void OnDidFinishLoadingStyle(MapStyle style)
        {
            try
            {
                Annotations = new ObservableCollection<Annotation>();
                Annotations.Add(new SymbolAnnotation
                {
                    Coordinates = new LatLng(50.4308233, 30.468171),
                    Id = Guid.NewGuid().ToString(),
                    Title = "test point",
                    SubTitle = "test point description",
                    IconImage = (ImageSource)"ic_pin8.png",
                    IsDraggable = false
                });
                Annotations.Add(new SymbolAnnotation
                {
                    Coordinates = new LatLng(50.4213206714367, 30.4711086026812),
                    Id = Guid.NewGuid().ToString(),
                    Title = "test point",
                    SubTitle = "test point description",
                    IconImage = (ImageSource)"ic_pin8.png",
                    IsDraggable = false
                });
                Annotations.Add(new SymbolAnnotation
                {
                    Coordinates = new LatLng(50.4392097647566, 30.4784340197486),
                    Id = Guid.NewGuid().ToString(),
                    Title = "test point",
                    SubTitle = "test point description",
                    IconImage = (ImageSource)"ic_pin8.png",
                    IsDraggable = false
                });
                Annotations.Add(new SymbolAnnotation
                {
                    Coordinates = new LatLng(50.4377507925521, 30.4584328331603),
                    Id = Guid.NewGuid().ToString(),
                    Title = "test point",
                    SubTitle = "test point description",
                    IconImage = (ImageSource)"ic_pin8.png",
                    IsDraggable = false
                });

            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.StackTrace);
                DisplayError(ex);
            }

        }