I have created a simple map application using OSM. Everything is working fine, but there is a problem when I am tring to use findViewById to denote the map in the layout.
I am getting this error:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.openstreetmaptutorial/com.example.openstreetmaptutorial.MainActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
The code of the activity:
public class MainActivity extends Activity {
double myCurrentLatitude;
double myCurrentLongitude;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//get current location
GPSTracker tracker = new GPSTracker(this);
if (tracker.canGetLocation() == false) {
tracker.showSettingsAlert();
} else {
myCurrentLatitude = tracker.getLatitude();
myCurrentLongitude = tracker.getLongitude();
}
MapView mapView = (MapView) findViewById(R.id.mapview);
mapView.setClickable(true);
mapView.setBuiltInZoomControls(true);
mapView.setTileSource(TileSourceFactory.MAPQUESTOSM);
mapView.getController().setZoom(15);
mapView.getController().setCenter(new GeoPoint(myCurrentLatitude, myCurrentLongitude));
List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.pin_for_map);
MapItemizedOverlay itemizedoverlay = new MapItemizedOverlay(drawable,this);
itemizedoverlay.setEnabled(true);
//setting pin to my current position on the map
GeoPoint homePoint = new GeoPoint(myCurrentLatitude,myCurrentLongitude);
OverlayItem overlayitem = new OverlayItem("Home Sweet Home", "name", homePoint);
mapView.getController().setZoom(15);
mapView.getController().setCenter(homePoint);
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
setContentView(mapView);
}
}
The Layout file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<org.osmdroid.views.MapView
android:id="@+id/mapview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:enabled="true"
android:clickable="true"
/>
</RelativeLayout>
The line which is actually causing the error is:
MapView mapView = (MapView) findViewById(R.id.mapview);
If I change the line to:
MapView mapView = new MapView(this, 256);
Then there is no error. But I need to implement the former one, as I want to add some more controls to the layout via xml. What should I do to overcome the problem? Ho can I use findViewById without the specified error?
The problem is occurring when you are calling setContentView()
method for the second time by passing mapView
setContentView(mapView);
The mapView
belongs to its parent RelativeLayout
. So, you are trying to set this mapView
as activity layout ignoring its parents RelativeLayout
then you are getting the IllegalStateException
...
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.openstreetmaptutorial/com.example.openstreetmaptutorial.MainActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
You can solve the problem by removing second setContentView()
method call.