locked
Error: Cannot add a null child view to a ViewGroup RRS feed

  • Question

  • User162155 posted

    I have an ArcGIS MapView in a page. In Android there is some bug where you have to remove the MapView from the UI when navigating to a new page, and add it back when you come back to your page...otherwise the map shows up on the page where it shouldn't and it blocks everything.

    See here: https://geonet.esri.com/thread/195773-navigation-between-2-mapviews-in-xamarin-android

    My issue is that I took their advice yet when I go to add the MapView back to the Grid I get this error "Cannot add a null child view to a ViewGroup".

    The Map is removed from the subsequent page as expected, however when I PopModalAsync and try to re-add the map it throws the error.

    During debugging neither my MapView nor my Grid are null. Any ideas on what I'm doing wrong?

    MapGrid is a Xamarin Forms Grid which is set to the Content of my ContentPage. MyDetailMapView is an ArcGIS MapView which contains the map. The MapGrid contains MyDetailMapView and a ScrollStack.

    Code here (could not get the format ticks to work right...sorry): `protected override void OnAppearing() { if (!MapGrid.Children.Contains(MyDetailMapView)) { MapGrid.Children.Add(MyDetailMapView); // <---- Exception is thrown here } base.OnAppearing(); }

    protected override void OnDisappearing()
    {
        if (MapGrid.Children.Contains(MyDetailMapView))
            MapGrid.Children.Remove(MyDetailMapView);
        base.OnDisappearing();
    }
    

    `

    Tuesday, August 29, 2017 10:59 PM

All replies

  • User180523 posted

    Maybe at full speed garbage collection is destroying the MapVIew? Could it be a global property scoped within app level and some other class retain an instance of it so it never goes out of scope?

    Wednesday, August 30, 2017 2:06 PM
  • User162155 posted

    @ClintStLaurent said: Maybe at full speed garbage collection is destroying the MapVIew? Could it be a global property scoped within app level and some other class retain an instance of it so it never goes out of scope?

    Even after the exception is thrown if I hover over MapView (while debugging) I see its contents, with a map that is "Loaded" with layers added and visibility attributes set.

    Is it possible for garbage collection to just destroy some part of an object that prevents it from being put into a ViewGroup?

    Wednesday, August 30, 2017 5:40 PM
  • User180523 posted

    After you remove the map you still see its contents... WTF? How can that be? if you remove it from the UI (visual tree) then it should be gone. period. Is this some sort of weird bait and switch where you place some sort of placeholder and the map is painting to the allocated block of screen real estate at a low driver level?
    Sounds like time to use a different map solution.

    Wednesday, August 30, 2017 5:54 PM
  • User162155 posted

    @ClintStLaurent said: After you remove the map you still see its contents... WTF? How can that be? if you remove it from the UI (visual tree) then it should be gone. period. Is this some sort of weird bait and switch where you place some sort of placeholder and the map is painting to the allocated block of screen real estate at a low driver level?
    Sounds like time to use a different map solution.

    Removing it from the UI should destroy the object? If it's a member of the ContentPage class shouldn't it still exist as long as that page exists?

    ArcGIS has a MapView object and a separate Map object. The MapView has a Map member that you assign to the ESRI map.

    I don't know how the internals of it work, but this exact solution was reported by at least 1 other person to have worked for them.
    This is where ESRI recommends doing this exact thing: https://geonet.esri.com/thread/195773-navigation-between-2-mapviews-in-xamarin-android

    Here I posed the original question and somebody responded that the ESRI solution worked (with 1 small tweak): https://stackoverflow.com/questions/44100605/arcgis-xamarin-mapview-from-previous-page-displays-after-navigating-to-new-pag/45884108#45884108

    Wednesday, August 30, 2017 6:24 PM
  • User180523 posted

    If it's a member of the ContentPage class shouldn't it still exist as long as that page exists? No. If you remove it from the ContentPage its gone... In real time. You don't have to reload the page. Same as adding a control to a page that is already displayed. You sort of see that on the page you linked to where they are removing the view from the parent [your parent layout].Children.Remove(MyMapView);

    So that leads to the question... What have you been doing when you say you've been "removing the map"?

    ArcGIS has a MapView object and a separate Map object. The MapView has a Map member that you assign to the ESRI map. Sounds exactly like the ~~Nokia~~ HERE MapFragment (MapView) that has a Map object inside the View.

    Wednesday, August 30, 2017 7:10 PM
  • User162155 posted

    @ClintStLaurent said:

    If it's a member of the ContentPage class shouldn't it still exist as long as that page exists? No. If you remove it from the ContentPage its gone... In real time. You don't have to reload the page. Same as adding a control to a page that is already displayed. You sort of see that on the page you linked to where they are removing the view from the parent [your parent layout].Children.Remove(MyMapView);

    So that leads to the question... What have you been doing when you say you've been "removing the map"?

    ArcGIS has a MapView object and a separate Map object. The MapView has a Map member that you assign to the ESRI map. Sounds exactly like the ~~Nokia~~ HERE MapFragment (MapView) that has a Map object inside the View.

    When I remove the map I do this:

    MapGrid.Children.Remove(MyDetailMapView);

    Whereas MapGrid is a Xamarin Forms grid that is set to the Content of my Content Page and MyDetailMapView is the MapView.

    If removing it from said grid destroys the object then the solution proposed by ESRI is nonsense right?

    It's weird that at least 1 person is reporting that it worked.

    So is there some other way to remove an object from the visual tree and re-add it?

    Thanks for your help btw, appreciate it immensely.

    Wednesday, August 30, 2017 10:03 PM
  • User180523 posted

    I personally feel that if you're having to add and remove controls through code behind, just to get them to work - I wouldn't keep working with them. Clearly they have issues. Sometimes you don't have a choice because the bosses tell you to find some way to make it work. If that's your situation, then I'm sorry.

    I've used Google maps and HERE maps - but not ArcGIS maps. I don't have I have much of an idea on what to do from here besides reaching out to the vendor. Sorry.

    Thursday, August 31, 2017 2:01 PM
  • User162155 posted

    @ClintStLaurent said: I personally feel that if you're having to add and remove controls through code behind, just to get them to work - I wouldn't keep working with them. Clearly they have issues. Sometimes you don't have a choice because the bosses tell you to find some way to make it work. If that's your situation, then I'm sorry.

    I've used Google maps and HERE maps - but not ArcGIS maps. I don't have I have much of an idea on what to do from here besides reaching out to the vendor. Sorry.

    Thanks for your help. The app was originally built on Google Maps and then a business decision was made to use ArcGIS.

    I'll try to circumvent the issue some other way.

    Thanks again.

    Thursday, August 31, 2017 3:28 PM