您好我正在使用本指南创建mapView和mapOverlays:http://developer.android.com/resources/tutorials/views/hello-mapview.html
这是我的代码:
我的mapoverlay(mapItem):
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
private Context mContext;
public MapItem(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}
public MapItem(Drawable defaultMarker, Context context) {
super(boundCenterBottom(defaultMarker));
mContext = context;
}
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
@Override
protected OverlayItem createItem(int i) {
return mOverlays.get(i);
}
@Override
public int size() {
return mOverlays.size();
}
//remove oldest element
public void remove(){
mOverlays.remove(0);
}
@Override
protected boolean onTap(int index) {
OverlayItem item = mOverlays.get(index);
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext); //row 50
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.show();
return true;
}
}
我的mapview类(mapViewActivity):
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mapview);
setupListener();
final MapView mapView = (MapView) findViewById(R.id.mapview);
mapController = mapView.getController();
mapView.setBuiltInZoomControls(true);
mapView.setClickable(true);
//Store resources
final List<Overlay> storeOverlays = mapView.getOverlays();
Drawable storePic = this.getResources().getDrawable(R.drawable.storemarker);
final MapItem itemizedStoreOverlay = new MapItem(storePic);
mapController.setZoom(13); // defualt zoom
//Plot all stores on the map
df = ((ClassHandler)getApplication()).getDatabaseFacade();
Cursor plotstore = df.getAllStorePos("Göteborg");
startManagingCursor(plotstore);
plotstore.moveToFirst();
while(plotstore.isAfterLast() == false){
GeoPoint addStore = new GeoPoint(plotstore.getInt(plotstore.getColumnIndex("lat")), plotstore.getInt(plotstore.getColumnIndex("long")));
OverlayItem overlayitem = new OverlayItem(addStore, plotstore.getString(plotstore.getColumnIndex("_id")), plotstore.getString(plotstore.getColumnIndex("address")));
itemizedStoreOverlay.addOverlay(overlayitem);
storeOverlays.add(itemizedStoreOverlay);
plotstore.moveToNext();
}
//Get current position
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
GeoPoint currentPos = new GeoPoint((int)(location.getLatitude()*1000000), (int)(location.getLongitude()*1000000));
mapController.animateTo(currentPos);
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status,
Bundle extras) {
// TODO Auto-generated method stub
}
};
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
//add current position
MyLocationOverlay myLocationOverlay = new MyLocationOverlay(this, mapView);
mapView.getOverlays().add(myLocationOverlay);
myLocationOverlay.enableMyLocation();
//myLocationOverlay.disableMyLocation();
}
private void setupListener() {
MapListener listener = new MapListener();
}
}
问题是,当我按下商店标记时,我的程序会因此错误而崩溃: http://elysobi.kjm.sgsnet.se:81/public/Android/ERROR.png
mapItem中的第50行标记在我的mapoverlay上。
任何人都知道问题是什么?
/感谢帮助,Ben。
答案 0 :(得分:1)
好像你调用了没有传递Context的错误的构造函数。
final MapItem itemizedStoreOverlay = new MapItem(storePic); // No context.