我正在制作一个显示带有 5 个连接坐标的谷歌地图的应用程序。我可以用图标标记它们。现在,当我们单击这些标记时,我试图显示一个对话框,但出现错误。请找到如下代码。任何帮助将非常感激。
这是我的主要课程。
public class MapConnect extends MapActivity implements LocationListener {
private static final String TAG = "LocationActivity";
// public List<Overlay> mapOverlays;
// public MyOwnLocationOverlay myLocationOverlay;
public static MapConnect instance;
public MapView mapView;
protected LocationManager locationManager;
public Button retrieveLocationButton;
Geocoder geocoder;
TextView locationText;
Location location;
MapController mapController;
CountDownTimer locationtimer;
GeoPoint point1 = new GeoPoint(29391748,13436279);
GeoPoint point2 = new GeoPoint(28835050,14128418);
GeoPoint point3 = new GeoPoint(28256006,13941650);
GeoPoint point4 = new GeoPoint(28401065,13029785);
GeoPoint point5 = new GeoPoint(29027355,12546387);
// private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters
// private static final long MINIMUM_TIME_BETWEEN_UPDATES = 9000; // in Milliseconds
// MapOverlay mapOverlay = new MapOverlay();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map_connect);
mapView = (MapView) findViewById(R.id.map_view);
MyLocationOverlay myLocationOverlay = new MyLocationOverlay(this, mapView);
mapView.getOverlays().add(myLocationOverlay);
mapView.postInvalidate();
myLocationOverlay.enableMyLocation();
mapView.setBuiltInZoomControls(true);
locationText = (TextView)this.findViewById(R.id.lblLocationInfo);
mapController = mapView.getController();
mapView.setSatellite(false);
mapController.setZoom(8);
retrieveLocationButton = (Button) findViewById(R.id.retrieve_location_button);
// GeoPoint point1 = new GeoPoint(29391748,13436279);
// GeoPoint point2 = new GeoPoint(28835050,14128418);
// GeoPoint point3 = new GeoPoint(28256006,13941650);
// GeoPoint point4 = new GeoPoint(28401065,13029785);
// GeoPoint point5 = new GeoPoint(29027355,12546387);
MapOverlay mapOvlay = new MapOverlay(point1, point2,point3,point4,point5);
//mapController.setCenter(point4);
mapView.getOverlays().add(mapOvlay);
List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.pin);
HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable);
OverlayItem overlayitem = new OverlayItem(point1, null, null);
OverlayItem overlayitem1 = new OverlayItem(point2, null, null);
OverlayItem overlayitem2 = new OverlayItem(point3, null, null);
OverlayItem overlayitem3 = new OverlayItem(point4, null, null);
OverlayItem overlayitem4 = new OverlayItem(point5, null, null);
itemizedoverlay.addOverlay(overlayitem);
itemizedoverlay.addOverlay(overlayitem1);
itemizedoverlay.addOverlay(overlayitem2);
itemizedoverlay.addOverlay(overlayitem3);
itemizedoverlay.addOverlay(overlayitem4);
mapOverlays.add(itemizedoverlay);
}
public class MapOverlay extends com.google.android.maps.Overlay {
private GeoPoint mGpt1;
private GeoPoint mGpt2;
private GeoPoint mGpt3;
private GeoPoint mGpt4;
private GeoPoint mGpt5;
protected MapOverlay(GeoPoint gp1, GeoPoint gp2,GeoPoint gp3,GeoPoint gp4,GeoPoint gp5 ) {
mGpt1 = gp1;
mGpt2 = gp2;
mGpt3 = gp3;
mGpt4 = gp4;
mGpt5 = gp5;
mapController.setCenter(gp3);
}
@Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
long when) {
super.draw(canvas, mapView, shadow);
Paint paint;
paint = new Paint();
paint.setColor(Color.RED);
paint.setAntiAlias(true);
paint.setStyle(Style.STROKE);
paint.setStrokeWidth(2);
Point pt1 = new Point();
Point pt2 = new Point();
Point pt3 = new Point();
Point pt4 = new Point();
Point pt5 = new Point();
Projection projection = mapView.getProjection();
projection.toPixels(mGpt1, pt1);
projection.toPixels(mGpt2, pt2);
projection.toPixels(mGpt3, pt3);
projection.toPixels(mGpt4, pt4);
projection.toPixels(mGpt5, pt5);
canvas.drawLine(pt1.x, pt1.y, pt2.x, pt2.y, paint);
canvas.drawLine(pt2.x, pt2.y, pt3.x, pt3.y, paint);
canvas.drawLine(pt3.x, pt3.y, pt4.x, pt4.y, paint);
canvas.drawLine(pt4.x, pt4.y, pt5.x, pt5.y, paint);
canvas.drawLine(pt5.x, pt5.y, pt1.x, pt1.y, paint);
return true;
}
}
// public void drawMyLocation(Canvas canvas,MapView mapView,location,point,Long 1000);
public void showsat(View view){
mapView.setSatellite(true);
}
public void showcurrentlocation(View view) {
// mapView.getOverlays().clear();
// mapOverlays = mapView.getOverlays();
//
// myLocationOverlay = new MyOwnLocationOverlay(GMapsActivity.this, mapView);
// myLocationOverlay.setMeters(10);
// myLocationOverlay.enableCompass();
// myLocationOverlay.enableMyLocation();
// myLocationOverlay.runOnFirstFix(new Runnable() {
// public void run() {
// mapController.animateTo(myLocationOverlay.getMyLocation());
// }
// });
// mapView.getOverlays().add(myLocationOverlay); }
mapView.setSatellite(false);
geocoder = new Geocoder(MapConnect.this);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
// add this overlay to the MapView and refresh it
if (location != null) {
Log.d(TAG, location.toString());
this.onLocationChanged(location); //<6>
}
}
//
@Override
public void onBackPressed() {
// do something on back.
MapOverlay mapOvlay = new MapOverlay(point1, point2,point3,point4,point5);
mapView.getOverlays().add(mapOvlay);
// finish();
return;
}
public void finish() {
// do something on back.
finish();
return;
}
@Override
public void onLocationChanged(Location location) {
Log.d(TAG, "onLocationChanged with location " + location.toString());
String text = String.format("Lat:\t %f\nLong:\t %f\nAlt:\t %f\nBearing:\t %f", location.getLatitude(),location.getLongitude(), location.getAltitude(),
location.getBearing());
this.locationText.setText(text);
try {
List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 10); //<10>
for (Address address : addresses) {
this.locationText.append("\n" + address.getAddressLine(0));
}
int latitude = (int)(location.getLatitude() * 1000000);
int longitude = (int)(location.getLongitude() * 1000000);
GeoPoint point = new GeoPoint(latitude,longitude);
mapController.animateTo(point); //<11>
}
catch (IOException e) {
Log.e("LocateMe", "Could not get Geocoder data", e);
}
//
}
@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
}
// @Override protected void onResume() {
// LocationListener locationListener = new LocationListener(){
// super.onResume();
// locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10, this); //<7>
// }
// @Override protected void onPause() {
// super.onPause();
// locationManager.removeUpdates(this); //<8>
//
// }
//@Override
//protected void onResume() {
// super.onResume();
//
// // when our activity resumes, we want to register for location updates
//
//
// myLocationOverlay.enableMyLocation();
//
//}
//
//@Override
//protected void onPause() {
// super.onPause();
//
// // when our activity pauses, we want to remove listening for location updates
//
// myLocationOverlay.disableMyLocation();
//
//}
@Override
protected boolean isRouteDisplayed() {
return false;
}
}
现在是 Itemized Overlay 类:
public class HelloItemizedOverlay extends ItemizedOverlay {
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
private Context mContext;
public HelloItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}
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();
}
// @Override
// protected boolean onTap(int index) {
// AlertDialog.Builder dialog = new AlertDialog.Builder(MapConnect.instance);
// switch (index) {
// case 0:
// Toast.makeText(MapConnect.instance, "GeoLocation : 0",
// Toast.LENGTH_LONG).show();
// dialog.show();
// break;
// }
//
// return true;
// }
@Override
protected boolean onTap(final int index) {
final OverlayItem id = mOverlays.get(index);
AlertDialog.Builder dialog = new AlertDialog.Builder(MapConnect.instance);
dialog.setTitle("your title");
dialog.setMessage("youmessage");
//dialog.setNegativeButton("Cancel", null);
dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int index) {
dialog.dismiss();
}
});
dialog.show();
return true;
}
}
这是我得到的 Logcat 错误:
2-06 07:17:34.822: E/InputEventReceiver(6112): Exception dispatching input event.
02-06 07:17:34.822: E/MessageQueue-JNI(6112): Exception in MessageQueue callback: handleReceiveCallback
02-06 07:17:34.893: E/MessageQueue-JNI(6112): java.lang.NullPointerException
02-06 07:17:34.893: E/MessageQueue-JNI(6112): at android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:143)
02-06 07:17:34.893: E/MessageQueue-JNI(6112): at android.app.AlertDialog$Builder.<init>(AlertDialog.java:360)
02-06 07:17:34.893: E/MessageQueue-JNI(6112): at com.example.mapconnect.HelloItemizedOverlay.onTap(HelloItemizedOverlay.java:58)
02-06 07:17:34.893: E/MessageQueue-JNI(6112): at com.google.android.maps.ItemizedOverlay.onTap(ItemizedOverlay.java:453)
02-06 07:17:34.893: E/MessageQueue-JNI(6112): at com.google.android.maps.OverlayBundle.onTap(OverlayBundle.java:83)
02-06 07:17:34.893: E/MessageQueue-JNI(6112): at com.google.android.maps.MapView$1.onSingleTapUp(MapView.java:358)
02-06 07:17:34.893: E/MessageQueue-JNI(6112): at com.google.android.maps.GestureDetector.onTouchEvent(GestureDetector.java:562)
02-06 07:17:34.893: E/MessageQueue-JNI(6112): at com.google.android.maps.MapView.onTouchEvent(MapView.java:685)
02-06 07:17:34.893: E/MessageQueue-JNI(6112): at android.view.View.dispatchTouchEvent(View.java:7239)
02-06 07:17:34.893: E/MessageQueue-JNI(6112): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2168)
02-06 07:17:34.893: E/MessageQueue-JNI(6112): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1903)
02-06 07:17:34.893: E/MessageQueue-JNI(6112): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
02-06 07:17:34.893: E/MessageQueue-JNI(6112): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
02-06 07:17:34.893: E/MessageQueue-JNI(6112): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
02-06 07:17:34.893: E/MessageQueue-JNI(6112): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
02-06 07:17:34.893: E/MessageQueue-JNI(6112): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
02-06 07:17:34.893: E/MessageQueue-JNI(6112): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
02-06 07:17:34.893: E/MessageQueue-JNI(6112): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
02-06 07:17:34.893: E/MessageQueue-JNI(6112): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)