ItemizedOverlay有以下代码:
private class AppointmentsOverlay extends ItemizedOverlay {
private List<OverlayItem> locations;
public AppointmentsOverlay(Drawable defaultMarker, List<Address> addresses) {
super(defaultMarker);
locations=new ArrayList<OverlayItem>();
convertAddressesToOverlayItems(addresses);
}
private void convertAddressesToOverlayItems(List<Address> addresses) {
for (Address address:addresses) {
GeoPoint gp=new GeoPoint((int)(address.getLatitude()*1000000),
(int)(address.getLongitude()*1000000));
locations.add(new OverlayItem(gp, "1", "1"));
}
}
@Override
protected OverlayItem createItem(int i) {
return locations.get(i);
}
@Override
public int size() {
return locations.size();
}
}
以及以下用于将此叠加层添加到MapView的代码:
private void initializeMapWithAppointments(List<Address> addresses) {
Drawable marker=getResources().getDrawable(R.drawable.marker);
mMapViewAppointments.getOverlays().add(new AppointmentsOverlay(marker, addresses));
//mMapViewAppointments.setClickable(false);
}
如果我点击MapView,我收到以下异常消息:
04-09 19:57:36.948: E/MapActivity(7053): Couldn't get connection factory client
04-09 19:57:41.263: E/AndroidRuntime(7053): FATAL EXCEPTION: main
04-09 19:57:41.263: E/AndroidRuntime(7053): java.lang.NullPointerException
04-09 19:57:41.263: E/AndroidRuntime(7053): at com.google.android.maps.ItemizedOverlay.getItemsAtLocation(ItemizedOverlay.java:617)
04-09 19:57:41.263: E/AndroidRuntime(7053): at com.google.android.maps.ItemizedOverlay.getItemAtLocation(ItemizedOverlay.java:586)
04-09 19:57:41.263: E/AndroidRuntime(7053): at com.google.android.maps.ItemizedOverlay.handleMotionEvent(ItemizedOverlay.java:498)
04-09 19:57:41.263: E/AndroidRuntime(7053): at com.google.android.maps.ItemizedOverlay.onTouchEvent(ItemizedOverlay.java:572)
04-09 19:57:41.263: E/AndroidRuntime(7053): at com.google.android.maps.OverlayBundle.onTouchEvent(OverlayBundle.java:63)
04-09 19:57:41.263: E/AndroidRuntime(7053): at com.google.android.maps.MapView.onTouchEvent(MapView.java:679)
04-09 19:57:41.263: E/AndroidRuntime(7053): at android.view.View.dispatchTouchEvent(View.java:5579)
04-09 19:57:41.263: E/AndroidRuntime(7053): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1959)
04-09 19:57:41.263: E/AndroidRuntime(7053): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1720)
04-09 19:57:41.263: E/AndroidRuntime(7053): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1965)
04-09 19:57:41.263: E/AndroidRuntime(7053): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1692)
04-09 19:57:41.263: E/AndroidRuntime(7053): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1965)
04-09 19:57:41.263: E/AndroidRuntime(7053): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1692)
04-09 19:57:41.263: E/AndroidRuntime(7053): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1965)
04-09 19:57:41.263: E/AndroidRuntime(7053): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1692)
04-09 19:57:41.263: E/AndroidRuntime(7053): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1965)
04-09 19:57:41.263: E/AndroidRuntime(7053): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1692)
04-09 19:57:41.263: E/AndroidRuntime(7053): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1983)
04-09 19:57:41.263: E/AndroidRuntime(7053): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1442)
04-09 19:57:41.263: E/AndroidRuntime(7053): at android.app.Activity.dispatchTouchEvent(Activity.java:2421)
04-09 19:57:41.263: E/AndroidRuntime(7053): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1931)
04-09 19:57:41.263: E/AndroidRuntime(7053): at android.view.View.dispatchPointerEvent(View.java:5759)
04-09 19:57:41.263: E/AndroidRuntime(7053): at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:2970)
04-09 19:57:41.263: E/AndroidRuntime(7053): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2529)
04-09 19:57:41.263: E/AndroidRuntime(7053): at android.view.ViewRootImpl.processInputEvents(ViewRootImpl.java:869)
04-09 19:57:41.263: E/AndroidRuntime(7053): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2538)
04-09 19:57:41.263: E/AndroidRuntime(7053): at android.os.Handler.dispatchMessage(Handler.java:99)
04-09 19:57:41.263: E/AndroidRuntime(7053): at android.os.Looper.loop(Looper.java:154)
04-09 19:57:41.263: E/AndroidRuntime(7053): at android.app.ActivityThread.main(ActivityThread.java:4945)
04-09 19:57:41.263: E/AndroidRuntime(7053): at java.lang.reflect.Method.invokeNative(Native Method)
04-09 19:57:41.263: E/AndroidRuntime(7053): at java.lang.reflect.Method.invoke(Method.java:511)
04-09 19:57:41.263: E/AndroidRuntime(7053): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-09 19:57:41.263: E/AndroidRuntime(7053): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-09 19:57:41.263: E/AndroidRuntime(7053): at dalvik.system.NativeStart.main(Native Method)
我不明白它的意思,因为我也看不到我的新自定义ItemizedOverlay。我添加了api密钥,我看到空MapView。请告诉我,我该如何解决?谢谢。
答案 0 :(得分:0)
在填充任何数据之前,您需要在ItemizedOverlay中调用populate()
。
public AppointmentsOverlay(Drawable defaultMarker, List<Address> addresses) {
super(defaultMarker);
populate(); // Add this
locations=new ArrayList<OverlayItem>();
convertAddressesToOverlayItems(addresses);
}
如所述here