Google Map Overlay Crashing

Put problem concerning MapActivities, MapViews, Overlays and GPS in this forum.

Google Map Overlay Crashing

Postby laxer311 » Sun Jun 05, 2011 4:17 am

I'm following a tutorial to create an overlay that puts a marker on the map at a specified location. The app keeps crashing with the code for the overlay, but runs fine without it. I'm not sure what I'm doing wrong here.

Code: Select all
import java.util.List;

import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.ZoomControls;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;
import com.google.android.maps.MapView.LayoutParams;


public class Map extends MapActivity implements LocationListener,
      OnClickListener {
   
   
   private GeoPoint carloc = new GeoPoint((int) (38.035489 * 1E6),
         (int) (-78.50347 * 1E6));
   
   /** Called when the activity is first created. */
   
   LocationManager location = null;
   MapView gmap = null;
   GeoPoint currentPoint = new GeoPoint(0, 0);

   private Criteria criteria;

   private MapController mc;
   private MapView mv;
   private LocationManager lm;
   private Double Latitude;
   private Double longitude;
   private int zoomlevel = 15;

   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main2);
      mv = (MapView) findViewById(R.id.mapView);
      mv.setSatellite(false);
      mc = mv.getController();
      
      MapOverlay mapOverlay = new MapOverlay();
      List<Overlay> listOfOverlays = mv.getOverlays();
      //listOfOverlays.clear();
      listOfOverlays.add(mapOverlay);
      
      mc.setZoom(zoomlevel);

      ZoomControls zoomControls = (ZoomControls) mv.getZoomControls();
      zoomControls.setLayoutParams(new ViewGroup.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

      mv.addView(zoomControls);
      mv.displayZoomControls(true);

      double lat = 38.0335 * 1E6;
      double longi = -78.507793 * 1E6;

      mc.setCenter(carloc);
   }
   
   class MapOverlay extends com.google.android.maps.Overlay
    {
        @Override
        public boolean draw(Canvas canvas, MapView mapView,
        boolean shadow, long when)
        {
            super.draw(canvas, mapView, shadow);                   

            //---translate the GeoPoint to screen pixels---
            Point screenPts = new Point();
            mapView.getProjection().toPixels(carloc, screenPts);

            //---add the marker---
            Bitmap bmp = BitmapFactory.decodeResource(
                getResources(), R.drawable.marker);           
            canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);         
            return true;
        }
    }


   @Override
   protected boolean isRouteDisplayed() {
      return false;
   }

   public void onClick(DialogInterface arg0, int arg1) {
      // TODO Auto-generated method stub
   }

   public void onLocationChanged(Location arg0) {
      // TODO Auto-generated method stub
      
   }

   public void onProviderDisabled(String arg0) {
      // TODO Auto-generated method stub
   }

   public void onProviderEnabled(String arg0) {
      // TODO Auto-generated method stub
   }

   public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
      // TODO Auto-generated method stub
   }
   

}


Code: Select all
06-05 03:42:04.641: ERROR/AndroidRuntime(9769): FATAL EXCEPTION: main
06-05 03:42:04.641: ERROR/AndroidRuntime(9769): java.lang.NullPointerException
06-05 03:42:04.641: ERROR/AndroidRuntime(9769):     at android.graphics.Canvas.throwIfRecycled(Canvas.java:954)
06-05 03:42:04.641: ERROR/AndroidRuntime(9769):     at android.graphics.Canvas.drawBitmap(Canvas.java:980)
06-05 03:42:04.641: ERROR/AndroidRuntime(9769):     at MapOverlay.draw(Map.java:99)
06-05 03:42:04.641: ERROR/AndroidRuntime(9769):     at com.google.android.maps.OverlayBundle.draw(OverlayBundle.java:42)
06-05 03:42:04.641: ERROR/AndroidRuntime(9769):     at com.google.android.maps.MapView.onDraw(MapView.java:494)
06-05 03:42:04.641: ERROR/AndroidRuntime(9769):     at android.view.View.draw(View.java:6740)
06-05 03:42:04.641: ERROR/AndroidRuntime(9769):     at android.view.ViewGroup.drawChild(ViewGroup.java:1640)
06-05 03:42:04.641: ERROR/AndroidRuntime(9769):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
06-05 03:42:04.641: ERROR/AndroidRuntime(9769):     at android.view.ViewGroup.drawChild(ViewGroup.java:1638)
06-05 03:42:04.641: ERROR/AndroidRuntime(9769):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
06-05 03:42:04.641: ERROR/AndroidRuntime(9769):     at android.view.View.draw(View.java:6743)
06-05 03:42:04.641: ERROR/AndroidRuntime(9769):     at android.widget.FrameLayout.draw(FrameLayout.java:352)
06-05 03:42:04.641: ERROR/AndroidRuntime(9769):     at android.view.ViewGroup.drawChild(ViewGroup.java:1640)
06-05 03:42:04.641: ERROR/AndroidRuntime(9769):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
06-05 03:42:04.641: ERROR/AndroidRuntime(9769):     at android.view.ViewGroup.drawChild(ViewGroup.java:1638)
06-05 03:42:04.641: ERROR/AndroidRuntime(9769):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
06-05 03:42:04.641: ERROR/AndroidRuntime(9769):     at android.view.View.draw(View.java:6743)
06-05 03:42:04.641: ERROR/AndroidRuntime(9769):     at android.widget.FrameLayout.draw(FrameLayout.java:352)
06-05 03:42:04.641: ERROR/AndroidRuntime(9769):     at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1842)
06-05 03:42:04.641: ERROR/AndroidRuntime(9769):     at android.view.ViewRoot.draw(ViewRoot.java:1407)
06-05 03:42:04.641: ERROR/AndroidRuntime(9769):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1163)
06-05 03:42:04.641: ERROR/AndroidRuntime(9769):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
06-05 03:42:04.641: ERROR/AndroidRuntime(9769):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-05 03:42:04.641: ERROR/AndroidRuntime(9769):     at android.os.Looper.loop(Looper.java:123)
06-05 03:42:04.641: ERROR/AndroidRuntime(9769):     at android.app.ActivityThread.main(ActivityThread.java:4627)
06-05 03:42:04.641: ERROR/AndroidRuntime(9769):     at java.lang.reflect.Method.invokeNative(Native Method)
06-05 03:42:04.641: ERROR/AndroidRuntime(9769):     at java.lang.reflect.Method.invoke(Method.java:521)
06-05 03:42:04.641: ERROR/AndroidRuntime(9769):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-05 03:42:04.641: ERROR/AndroidRuntime(9769):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-05 03:42:04.641: ERROR/AndroidRuntime(9769):     at dalvik.system.NativeStart.main(Native Method)
laxer311
Once Poster
Once Poster
 
Posts: 1
Joined: Sun Jun 05, 2011 3:03 am

Top

Return to Map Problems

Who is online

Users browsing this forum: Google Feedfetcher and 3 guests