0

我正在阅读 Web 服务教程,并遇到了我无法修复的最后一个错误。

当调用构造函数 OverlayItem(Drawable) 未定义的类构造函数时,我在 itemOverlay 对象上收到错误。

我真的不明白这一点,因为我正在向构造函数提供相关数据,我不敢相信构造函数不需要任何值传递给它?

这是发生错误的方法:

 MapActivity mapAct = (MapActivity)ctx;
        MapView map = (MapView)mapAct.findViewById(R.id.map);
        map.setScrollBarStyle(MapView.SCROLLBARS_INSIDE_INSET);
        map.setBuiltInZoomControls(Boolean.TRUE);
        map.displayZoomControls(Boolean.TRUE);

        GeoPoint point = new GeoPoint((int)(geoName.lat * 1E6), (int)(geoName.lng * 1E6));
        MapController mc = map.getController();
        mc.setZoom(17);
        mc.setCenter(point);
        mc.animateTo(point);

        List<Overlay> overlays = map.getOverlays();
        overlays.clear();

                    ***** ERROR on constructor*****
        OverlayItem items = new OverlayItem(ctx.getResources().getDrawable(R.drawable.marker));
        OverlayItem item = new OverlayItem(point, geoName.placeName, "" + geoName.postalCode);

        items.addOverlay(item);
        overlays.add(items);
4

1 回答 1

0

It's not clear why you're confused. Look at the OverlayItem documentation, and there's just a single constructor:

public OverlayItem(GeoPoint point,
                   java.lang.String title,
                   java.lang.String snippet)

So your second constructor call (for item) is fine, as that's passing in a point, title and snippet, but this one (for items):

new OverlayItem(ctx.getResources().getDrawable(R.drawable.marker))

is trying to just pass a Drawable... and there's no constructor with a siganture of

// This doesn't exist!
public OverlayItem(Drawable drawable)

Given the rest of your code, I suspect you actually wanted ItemizedOverlay:

ItemizedOverlay items = new ItemizedOverlay(ctx.getResources().getDrawable(R.drawable.marker));

Rather than just change your code, you should take a step back and work out why you didn't spot this from the compiler error. It's giving you all the information you need to understand why your current code is wrong - although admittedly not the class that you should have been using instead.

于 2013-02-20T16:58:20.820 回答