Исключение нулевого указателя при использовании ItemizedOverlay с MapView - PullRequest
0 голосов
/ 21 марта 2012

Я получаю ошибки ниже при касании вида карты. 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)

Код, который добавляет пунктированные оверлеи:

// 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();

Мой предметный оверлейный класс:

@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;
    }
}

1 Ответ

4 голосов
/ 21 марта 2012

Я решил проблему.Ошибка возникала, когда в просмотре карты был наложенный элемент, который не имел элементов наложения.Следующий код исправил ошибку:

if(itemizedoverlayCurrent.hasItems()) overlays.add(itemizedoverlayCurrent); 
if(itemizedoverlayCompleted.hasItems())overlays.add(itemizedoverlayCompleted);  
if(itemizedoverlayFuture.hasItems())overlays.add(itemizedoverlayFuture);
...