触摸mapview时,我会收到以下错误消息。 ItemizedOverlay显示正确,但我无法与之交互。一直在网上搜索解决方案,但找不到任何解决方案。
logcat的:
03-03 21:32:47.278: E/AndroidRuntime(18467): java.lang.NullPointerException
03-03 21:32:47.278: E/AndroidRuntime(18467): at com.google.android.maps.ItemizedOverlay.getItemsAtLocation(ItemizedOverlay.java:617)
03-03 21:32:47.278: E/AndroidRuntime(18467): at com.google.android.maps.ItemizedOverlay.getItemAtLocation(ItemizedOverlay.java:586)
03-03 21:32:47.278: E/AndroidRuntime(18467): at com.google.android.maps.ItemizedOverlay.handleMotionEvent(ItemizedOverlay.java:498)
03-03 21:32:47.278: E/AndroidRuntime(18467): at com.google.android.maps.ItemizedOverlay.onTouchEvent(ItemizedOverlay.java:572)
03-03 21:32:47.278: E/AndroidRuntime(18467): at com.google.android.maps.OverlayBundle.onTouchEvent(OverlayBundle.java:63)
03-03 21:32:47.278: E/AndroidRuntime(18467): at com.google.android.maps.MapView.onTouchEvent(MapView.java:643)
03-03 21:32:47.278: E/AndroidRuntime(18467): at android.view.View.dispatchTouchEvent(View.java:3766)
03-03 21:32:47.278: E/AndroidRuntime(18467): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:897)
03-03 21:32:47.278: E/AndroidRuntime(18467): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
03-03 21:32:47.278: E/AndroidRuntime(18467): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
03-03 21:32:47.278: E/AndroidRuntime(18467): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
03-03 21:32:47.278: E/AndroidRuntime(18467): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
03-03 21:32:47.278: E/AndroidRuntime(18467): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
03-03 21:32:47.278: E/AndroidRuntime(18467): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
03-03 21:32:47.278: E/AndroidRuntime(18467): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
03-03 21:32:47.278: E/AndroidRuntime(18467): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1671)
03-03 21:32:47.278: E/AndroidRuntime(18467): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
03-03 21:32:47.278: E/AndroidRuntime(18467): at android.app.Activity.dispatchTouchEvent(Activity.java:2086)
03-03 21:32:47.278: E/AndroidRuntime(18467): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1655)
03-03 21:32:47.278: E/AndroidRuntime(18467): at android.view.ViewRoot.handleMessage(ViewRoot.java:1785)
03-03 21:32:47.278: E/AndroidRuntime(18467): at android.os.Handler.dispatchMessage(Handler.java:99)
03-03 21:32:47.278: E/AndroidRuntime(18467): at android.os.Looper.loop(Looper.java:123)
03-03 21:32:47.278: E/AndroidRuntime(18467): at android.app.ActivityThread.main(ActivityThread.java:4627)
03-03 21:32:47.278: E/AndroidRuntime(18467): at java.lang.reflect.Method.invokeNative(Native Method)
03-03 21:32:47.278: E/AndroidRuntime(18467): at java.lang.reflect.Method.invoke(Method.java:521)
03-03 21:32:47.278: E/AndroidRuntime(18467): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-03 21:32:47.278: E/AndroidRuntime(18467): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-03 21:32:47.278: E/AndroidRuntime(18467): at dalvik.system.NativeStart.main(Native Method)
添加itemizedoverlays的代码:
// Add event markers
List<Overlay> overlays = mapView.getOverlays();
overlays.clear();
CustomItemizedOverlay itemizedoverlayCurrent , itemizedoverlayCompleted, itemizedoverlayFuture;
GeoPoint point;
// Add events
Cursor cursor = mDbHelper.fetchAllEvents();
Drawable drawable = this.getResources().getDrawable(R.drawable.androidmarker);
itemizedoverlayCurrent = new CustomItemizedOverlay(drawable, this);
drawable = this.getResources().getDrawable(R.drawable.ic_launcher);
itemizedoverlayCompleted = new CustomItemizedOverlay(drawable, this);
drawable = this.getResources().getDrawable(R.drawable.androidmarker);
itemizedoverlayFuture = new CustomItemizedOverlay(drawable, this);
while(cursor.moveToNext()){
Bundle b = new Bundle();
b.putLong(EventDbAdapter.COLUMN_ID, cursor.getLong(cursor.getColumnIndex(EventDbAdapter.COLUMN_ID)));
point = new GeoPoint(cursor.getInt(cursor.getColumnIndex(EventDbAdapter.COLUMN_EVENTS_START_LAT)), cursor.getInt(cursor.getColumnIndex(EventDbAdapter.COLUMN_EVENTS_START_LONG)));
CustomOverlayItem overlayitem = new CustomOverlayItem(point, cursor.getString(cursor.getColumnIndex(EventDbAdapter.COLUMN_EVENTS_TITLE)), null, CustomOverlayItem.EVENT_TYPE, b);
if(cursor.getInt(cursor.getColumnIndex(EventDbAdapter.COLUMN_EVENTS_TYPE)) == EventDbAdapter.EVENT_TYPE_COMPLETED) itemizedoverlayCompleted.addOverlay(overlayitem);
else if(cursor.getInt(cursor.getColumnIndex(EventDbAdapter.COLUMN_EVENTS_TYPE)) == EventDbAdapter.EVENT_TYPE_CURRENT) itemizedoverlayCurrent.addOverlay(overlayitem);
else if(cursor.getInt(cursor.getColumnIndex(EventDbAdapter.COLUMN_EVENTS_TYPE)) == EventDbAdapter.EVENT_TYPE_FUTURE) itemizedoverlayFuture.addOverlay(overlayitem);
}
overlays.add(itemizedoverlayCurrent);
overlays.add(itemizedoverlayCompleted);
overlays.add(itemizedoverlayFuture);
mapView.postInvalidate();
我的itemizedlaylay类:
@SuppressWarnings("rawtypes")
public class CustomItemizedOverlay extends ItemizedOverlay {
private ArrayList<CustomOverlayItem> mOverlays = new ArrayList<CustomOverlayItem>();
private Context mContext;
public CustomItemizedOverlay(Drawable defaultMarker) {super(boundCenterBottom(defaultMarker));}
public CustomItemizedOverlay(Drawable defaultMarker, Context context) {
super(boundCenterBottom(defaultMarker));
mContext = context;
}
public void addOverlay(CustomOverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
protected OverlayItem createItem(int i) {return mOverlays.get(i);}
public int size() {return mOverlays.size();}
protected boolean onTap(int index) {
if(size() > 0){
CustomOverlayItem i = mOverlays.get(index);
Bundle b = i.getBundle();
switch(i.getType()){
case CustomOverlayItem.EVENT_TYPE:
Toast.makeText(mContext, "EVENT", Toast.LENGTH_SHORT).show();
break;
case CustomOverlayItem.IMAGE_TYPE:
break;
case CustomOverlayItem.VIDEO_TYPE:
break;
case CustomOverlayItem.CONTACT_TYPE:
break;
case CustomOverlayItem.AUDIO_TYPE:
break;
case CustomOverlayItem.UNKNOWN_TYPE:
break;
default:
break;
}
}
return true;
}
}
答案 0 :(得分:4)
我已经解决了这个问题。当mapview上有一个没有重叠项的itemized覆盖时,会出现错误。以下代码解决了错误:
if(itemizedoverlayCurrent.hasItems()) overlays.add(itemizedoverlayCurrent);
if(itemizedoverlayCompleted.hasItems())overlays.add(itemizedoverlayCompleted);
if(itemizedoverlayFuture.hasItems())overlays.add(itemizedoverlayFuture);