0

Hi everyone I'm trying to draw polylines between two points on the map but when I run my method it gives the errors listed below. I've narrowed it down the erroring line to:

canvas.drawLine(p1.x, p1.y, p2.x, p2.y, mPaint);

here is my class that is trying to draw

package com.example.hellogooglemap;

class MyOverlay extends Overlay{
  private Projection projection;
  Paint mPaint;
  Context mContext;
  List<GeoPoint> gpList;


public MyOverlay(List<GeoPoint> geopointList, Context context) {
      super();
      mContext = context;
      gpList = geopointList;

      Paint   mPaint = new Paint();
      mPaint.setARGB(255, 10, 10, 255);
      mPaint.setAntiAlias(true);
      mPaint.setFakeBoldText(true); 

        //Toast.makeText(mContext, gpList.get(0).toString() , Toast.LENGTH_SHORT).show();
}

@Override
public void draw(Canvas canvas, MapView mapv, boolean shadow){  
    projection = mapv.getProjection();

    Point p1 = new Point();
    Point p2 = new Point();        

    projection.toPixels(gpList.get(0), p1);
    projection.toPixels(gpList.get(1), p2);

    canvas.drawLine(p1.x, p1.y, p2.x, p2.y, mPaint);        

    super.draw(canvas, mapv, shadow);
    }

}  

here are the errors

12-08 03:50:05.559: E/AndroidRuntime(1804): FATAL EXCEPTION: main
12-08 03:50:05.559: E/AndroidRuntime(1804): java.lang.NullPointerException
12-08 03:50:05.559: E/AndroidRuntime(1804):     at android.graphics.Canvas.drawLine(Canvas.java:876)
12-08 03:50:05.559: E/AndroidRuntime(1804):     at com.example.hellogooglemap.MyOverlay.draw(MyOverlay.java:56)
12-08 03:50:05.559: E/AndroidRuntime(1804):     at com.google.android.maps.Overlay.draw(Overlay.java:179)
12-08 03:50:05.559: E/AndroidRuntime(1804):     at com.google.android.maps.OverlayBundle.draw(OverlayBundle.java:42)
12-08 03:50:05.559: E/AndroidRuntime(1804):     at com.google.android.maps.MapView.onDraw(MapView.java:532)
12-08 03:50:05.559: E/AndroidRuntime(1804):     at android.view.View.draw(View.java:13707)
12-08 03:50:05.559: E/AndroidRuntime(1804):     at android.view.View.draw(View.java:13591)
12-08 03:50:05.559: E/AndroidRuntime(1804):     at android.view.ViewGroup.drawChild(ViewGroup.java:2928)
12-08 03:50:05.559: E/AndroidRuntime(1804):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797)
12-08 03:50:05.559: E/AndroidRuntime(1804):     at android.view.View.draw(View.java:13589)
12-08 03:50:05.559: E/AndroidRuntime(1804):     at android.view.ViewGroup.drawChild(ViewGroup.java:2928)

any help would be greatly appreciated. I've been stuck on this for a good while.

Siddharth
  • 9,349
  • 16
  • 86
  • 148
lululoo
  • 163
  • 2
  • 14

2 Answers2

1

In this line:

Paint   mPaint = new Paint();

You are assigning to a local variable, not to the class one. Just remove the leading "Paint"

mPaint = new Paint();
yoah
  • 7,180
  • 2
  • 30
  • 30
0

Maybe you could try Google Maps Android API v2, as v1 is deprecated.

mbarrben
  • 372
  • 3
  • 9