0

我试图用我的构造函数解决这个问题,它给了我一个重复的方法错误:

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;


public class MapRouteOverlay extends Overlay {
private GeoPoint gp1;
private GeoPoint gp2;

private int mode=0;
private int defaultColor;

public MapRouteOverlay(GeoPoint gp1, GeoPoint gp2, int mode) { 
    this.gp1 = gp1;
    this.gp2 = gp2;
    this.mode = mode;
    defaultColor = 999;
}

public MapRouteOverlay(GeoPoint gp1, GeoPoint gp2, int defaultColor) { 
    this.gp1 = gp1;
    this.gp2 = gp2;
    this.mode = mode;
    this.defaultColor = defaultColor;
}

public int getMode() { 
    return mode;
}

public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) { 
    Projection projection = mapView.getProjection(); 
    if (shadow == false) { 
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        Point point = new Point();
        projection.toPixels(gp1, point);

        if (mode==2) {
            if(defaultColor == 999)
                paint.setColor(Color.RED);
            else 
                paint.setColor(defaultColor);
            Point point2 = new Point();
            projection.toPixels(gp2, point2);
            paint.setStrokeWidth(5);
            paint.setAlpha(120);
            canvas.drawLine(point.x, point.y, point2.x, point2.y, paint);
        }
    }
    return super.draw(canvas, mapView, shadow, when);
}
}

我得到的错误是:Duplicate method MapRouteOverlay(GeoPoint, GeoPoint, int) in type MapRouteOverlay

我希望他们成为构造函数。我该如何解决这个问题才能正常工作?任何帮助,将不胜感激!

4

1 回答 1

0

你不满足重载规则。

public MapRouteOverlay(GeoPoint gp1, GeoPoint gp2, int mode) { 

您有一个具有相同数量和类型的参数的重复构造函数。

java中方法重载的规则(也适用于构造函数):

  1. 必须有不同的参数列表。
  2. 可能抛出不同的异常
  3. 可能有不同的返回类型(在方法重载的情况下)

您将不得不像这样更改您的构造函数签名之一:

public MapRouteOverlay( int mode,GeoPoint gp1, GeoPoint gp2) { 
      this(gp1,gp2, mode); //if your super class does not have an no-args constructor,this must be the first line in an overloaded constructor, a call to this() or super();
      //rest of your code.
  }
于 2012-11-20T21:27:45.763 回答