AnsweredAssumed Answered

MapFragment ArcGis - The specified child already has a parent

Question asked by urbinax on Jul 14, 2015
Latest reply on Jul 14, 2015 by urbinax

I'm trying to display a MapView from ArcGis in Android Studio. I'm using Map Fragments for that, I put in the xml the Map and my other UI stuff. Putting the MapView element in the layout file always crashes, so I'm doing it this way (using android studio):

my_main_activity.xml  < xmlns:android="" android:id="@+id/maps_app_activity_drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" >  <!-- The main content view -->  <FrameLayout android:id="@+id/maps_app_activity_content_frame" android:layout_width="match_parent" android:layout_height="match_parent" />  <!-- The navigation drawer --> <ListView android:id="@+id/maps_app_activity_left_drawer" style="@style/drawer_listView_style" android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="start" android:background="@android:color/darker_gray" android:choiceMode="singleChoice" android:divider="@color/esri_gray" android:dividerHeight="1px" />

and this is the framgent layout: map_fragment_layout.xml

<FrameLayout xmlns:android="" android:id="@+id/map_fragment_map_container_frame_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- The map configuration --> < android:id="@+id/map_layout" android:layout_width="fill_parent" android:layout_height="fill_parent" initExtent = "-9934033.827 1537316.31 -9933312.043 1537940.728" > <!-- xmin ymin xmax ymax --> </>

My map fragment class holds every configuration of the MapView:

@Override public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { mMapContainer = (FrameLayout) inflater.inflate(R.layout.map_fragment_layout,container,false);  // Add dynamic layer to MapView (Base) // Retrieve the map and initial extent from XML layout MapView mapView = (MapView)mMapContainer.findViewById(; ArcGISDynamicMapServiceLayer baseMap = new ArcGISDynamicMapServiceLayer(baseMapURL); mapView.addLayer(baseMap); //Creates a dynamic layer using service URL ArcGISDynamicMapServiceLayer dynamicLayer = new ArcGISDynamicMapServiceLayer(dynamicMapURL); //Adds layer into the 'MapView' mapView.addLayer(dynamicLayer);  // Set the MapView to allow the user to rotate the map when as part of a pinch gesture. setMapView(mapView); mapView.zoomin();  return mMapContainer; }

the function setMapView configures the rest of MapView options:

private void setMapView(final MapView mapView) {  mMapView = mapView; mMapView.setEsriLogoVisible(false); mMapView.enableWrapAround(true); mapView.setAllowRotationByPinch(true);  // Creating an inflater mInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);  // Setting up the layout params for the searchview and searchresult layout mlayoutParams = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT, Gravity.LEFT | Gravity.TOP); mlayoutParams.setMargins(LEFT_MARGIN_SEARCH, TOP_MARGIN_SEARCH,RIGHT_MARGIN_SEARCH, BOTTOM_MARGIN_SEARCH);  // set MapView into the activity layout mMapContainer.addView(mMapView);  // Displaying the searchbox layout showSearchBoxLayout();

After debuging this error shows up:

java.lang.RuntimeException: Unable to start activity ComponentInfo{zero.ucamaps/zero.ucamaps.MainActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.  at at at$800( at$H.handleMessage( at android.os.Handler.dispatchMessage( at android.os.Looper.loop( at at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke( at$ at Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. at android.view.ViewGroup.addViewInner( at android.view.ViewGroup.addView( at android.view.ViewGroup.addView( at android.view.ViewGroup.addView( at zero.ucamaps.MapFragment.setMapView( at zero.ucamaps.MapFragment.onCreateView( at

I've tried removing the the MapView from it's parent before returning from getMapView(), and that still crashes. Any help at all would be appreciated.