Android MapView trying to add a view but already has a parent
I have the following code:
setContentView(R.layout.maplayout);
// Create Rotate view
mRotateView = new RotateView(this);
// create a map view
mapView = (MapView) findViewById(R.id.mapview);
mRotateView.addView(mapView); // error here
setContentView(mRotateView);
mylocation = new MyLocationOverlay(this,mapView);
But when I add the view, I get an error that says the specified child already has a parent. I assume this is because the mapView is already a child of the layout.
So how do I solve this?
source to share
Option # 1: Place RotateView
in your resource res/layout/maplayout.xml
as a parent of the widget MapView
so you don't need to set it in Java code.
Option # 2: Remove MapView
from your resource res/layout/maplayout.xml
, instantiate it through your constructor and add it to your RotateView
Java code .
Option # 3: Call the removeView()
parent to remove from it MapView
before calling addView()
to add it to RotateView
.
source to share