java - Error while using "findViewById" for mapview -


I have created a simple map application using the OSM. Everything is working fine, but there is a problem when I'm trying to use findViewById to map to the layout.

I Error :

  java.lang.RuntimeException: Unable to start activity ComponentInfo {com.example.openstreetmaptutorial / com.example .openstreetmaptutorial.MainActivity}: java.lang.IllegalStateException: The specified child is already the mother or father. You must first call the removeView () on the child's parent   

Activity code :

  Public class main activity activity increases (double myCurrentLatitude; double Merilgutantrn: @ override protected void saved create (bundled Instansstet) {Suprknkret (saved Instensstet); receive setContentView (R.layout.activity_main); // current location GPSTracker Tracker = New GPSTracker (this); If (tracker.canGetLocation () == incorrect) {tracker.showSettingsAlert ();} Other {my CurrentLatitude = tracker.getLatitude (); MyCurrentLongitude = tracker.getLongitude ();} MapView mapView = (MapView) findViewById (R.id.mapview); mapView.setClickable (right); mapView.setBuiltInZoomControls (right); mapView.setTileSource ( TileSourceFactory.MAPQUESTOSM); mapView.getController () setZoom (15). MapView.getController (). SetCenter (new GeoPoint (myCurrentLatitude, myCurrentLongitude)); list & lt; overlay & gt; Nkshaovrle = Nkshaviavet. overlay (); drawable Drauble = It. Gareth's Source (). GetDrawable (R.drawable.pin_for_map); Mapimeted overlay item doneOverval = new mapimated overlay (droplet, this); Itemizedoverlay.setEnabled (true); // Set to pin my current status on the map GeoPoint HomePoint = New GeoPoint (myCurrentLatitude, myCurrentLongitude); Overlayist overlayem = new overlayem ("Home Sweet Home", "Name", HomePoint); MapView.getController () setZoom (15) MapView.getController () setCenter (homepoint). Itemizedoverlay.addOverlay (overlayitem); MapOverlays.add (itemizedoverlay); setContentView (MapView); }}   

layout file :

  & lt; Relative layout xmlns: android = "http://schemas.android com / apk / res / android" android: layout_width = "match_parent" android: layout_height = "match_parent" & gt; & Lt; org.osmdroid.views.MapView Android: Eddie = "@ + id / Mapviwu" Android: Leaut_vidth = "Mac_perent" Android: Leaut_heit = "Mac_perent" Android-enabled = "true" android: clickable = "true" / & Gt; & Lt; / RelativeLayout & gt; The line that generates the error is:  

Map Map Map = (MapView) SearchVBIID (RIDMapView);

If I change this line:

Mapview map view = new map view (this, 256);

Error. But I need to apply the first one, because I want to add some more control to layout through XML. What should I do to overcome the problem? Can I use findViewById without a specific error?

The problem is happening when you mapView

 Passing the  setContentView (mapView) call for the second time the  setContentView ()  method;   

mapView belongs to its parent relative layout . Therefore, you are trying to ignore this mapView to your parent relative layout , then you are receiving IllegalStateException . ..

  java.lang.RuntimeException: Unable to start activity ComponentInfo {com.example.openstreetmaptutorial / com.example.openstreetmaptutorial.MainActivity}: java.lang.IllegalStateException: Specified child's Already have a parent. You must first call the removeView () on the child's parent   

You can solve the problem by removing the second setContentView () method call.

Comments

Popular posts from this blog

Java - Error: no suitable method found for add(int, java.lang.String) -

java - JPA TypedQuery: Parameter value element did not match expected type -

c++ - static template member variable has internal linkage but is not defined -