39

I have been trying for hours to figure out why I can not use an Itemized Map overlay without doing this before adding it to the ovelays of the map:

GeoPoint point; OverlayItem overlayitem = new OverlayItem(point, "",""); MarkerOverlay.addOverlay(overlayitem);

If i try to do this without the MarkerOverlay.addOverlay(overlayitem); command then when i try to add a new overLay like so:

public boolean onTap(GeoPoint point, MapView mapView)
{
    if (mOverlays.size() > 0)
        mOverlays.remove(0);
    OverlayItem overlayitem = new OverlayItem(point, "", "");
    mOverlays.add(overlay);
    populate();
    return true;
}

Inside my ItemizedOverlay class then i Just get a null reference exeption (I have no idea when or why - It says its no source code available when in debug)

Any Ideas?

EDIT: This is what I can find in the logcat :

09-20 22:36:22.164: WARN/dalvikvm(311): threadid=3: thread exiting with uncaught exception (group=0x4001b188)

and this is what I believe to be the infamous exception:

   09-20 22:36:22.293: ERROR/AndroidRuntime(311): java.lang.NullPointerException
09-20 22:36:22.293: ERROR/AndroidRuntime(311):     at com.google.android.maps.ItemizedOverlay.getItemsAtLocation(ItemizedOverlay.java:617)
09-20 22:36:22.293: ERROR/AndroidRuntime(311):     at com.google.android.maps.ItemizedOverlay.getItemAtLocation(ItemizedOverlay.java:586)
09-20 22:36:22.293: ERROR/AndroidRuntime(311):     at com.google.android.maps.ItemizedOverlay.handleMotionEvent(ItemizedOverlay.java:498)
09-20 22:36:22.293: ERROR/AndroidRuntime(311):     at com.google.android.maps.ItemizedOverlay.onTouchEvent(ItemizedOverlay.java:572)
09-20 22:36:22.293: ERROR/AndroidRuntime(311):     at com.google.android.maps.OverlayBundle.onTouchEvent(OverlayBundle.java:63)
09-20 22:36:22.293: ERROR/AndroidRuntime(311):     at com.google.android.maps.MapView.onTouchEvent(MapView.java:625)
09-20 22:36:22.293: ERROR/AndroidRuntime(311):     at android.view.View.dispatchTouchEvent(View.java:3709)
09-20 22:36:22.293: ERROR/AndroidRuntime(311):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:852)
09-20 22:36:22.293: ERROR/AndroidRuntime(311):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:822)
09-20 22:36:22.293: ERROR/AndroidRuntime(311):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:822)
09-20 22:36:22.293: ERROR/AndroidRuntime(311):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:822)
09-20 22:36:22.293: ERROR/AndroidRuntime(311):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
09-20 22:36:22.293: ERROR/AndroidRuntime(311):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
09-20 22:36:22.293: ERROR/AndroidRuntime(311):     at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
09-20 22:36:22.293: ERROR/AndroidRuntime(311):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
09-20 22:36:22.293: ERROR/AndroidRuntime(311):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
09-20 22:36:22.293: ERROR/AndroidRuntime(311):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-20 22:36:22.293: ERROR/AndroidRuntime(311):     at android.os.Looper.loop(Looper.java:123)
09-20 22:36:22.293: ERROR/AndroidRuntime(311):     at android.app.ActivityThread.main(ActivityThread.java:4363)
09-20 22:36:22.293: ERROR/AndroidRuntime(311):     at java.lang.reflect.Method.invokeNative(Native Method)
09-20 22:36:22.293: ERROR/AndroidRuntime(311):     at java.lang.reflect.Method.invoke(Method.java:521)
09-20 22:36:22.293: ERROR/AndroidRuntime(311):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
09-20 22:36:22.293: ERROR/AndroidRuntime(311):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
09-20 22:36:22.293: ERROR/AndroidRuntime(311):     at dalvik.system.NativeStart.main(Native Method)
09-20 22:36:22.394: ERROR/dalvikvm(311): Unable to open stack trace file '/data/anr/traces.txt': Permission denied
Jason
  • 4,034
  • 4
  • 40
  • 62

3 Answers3

115

I recently came across this problem. The issue is outlined in this bug report.

To fix it you should call populate() in your ItemizedOverlay before any data is populated. I added it to the constructor:

private class MyItemizedOverlay extends ItemizedOverlay<OverlayItem> {

    private Context context;
    private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();

    LocationItemizedOverlay(Drawable defaultMarker, Context context) {
        super(boundCenterBottom(defaultMarker));
        this.context = context;
        populate(); // Add this
    }
}
Che
  • 1,691
  • 1
  • 15
  • 8
0

if you make important changes on your ItemizedOverlay you have every time to do

  1. delete_usrer_Marker_from_Overlays(myitemizedoverlay);
  2. create_user_Marker(myitemizedoverlay);
  3. mapView.getOverlays().add(myitemizedoverlay);

with

private void delete_usrer_Marker_from_Overlays(MyOverlays myio){

    List<Overlay> mapOverlays = mapView.getOverlays();
    if (mapOverlays != null) 
        {
          for (int i = 0;i< mapOverlays.size();i++)
          {
              Overlay x = mapOverlays.get(i);
              if (x.hashCode() == myio.hashCode())
              {
                  mapOverlays.remove(x);
              }                     
          }
        }       
    }
Ingo
  • 5,239
  • 1
  • 30
  • 24
0

The itemized overlay needs the 2nd 2 paramters because it uses them for click events. When you click one of the itemized overlays, it has a title and a description associated with it, which are the 2nd and 3rd parameters in your overlay item

Falmarri
  • 47,727
  • 41
  • 151
  • 191
  • The itemized overlay works but only if I add a random point onCreate of the mapview else it crashes – Jason Sep 21 '10 at 05:18
  • Oh I misunderstood. What's your question then? Your post is really confusing. Can you post the exact code that works, and then the exact code that doesn't? – Falmarri Sep 21 '10 at 05:56
  • Its a mess to post the exact code becuse its in a few files. Basicly if i Make an iteized Overlay And add it to the Overlays of the map without adding a Overlay Item before adding it to the overlays of the map. then when I try create a new overlayItem (I am doing it on the onTap Method) then the application crashes as posted. – Jason Sep 21 '10 at 16:55
  • Why do you need to add an itemized overlay without any overlay items to a map? – Falmarri Sep 21 '10 at 21:20
  • Just until the user presses the map then the Item goes to that location – Jason Oct 05 '10 at 21:16
  • Are you sure you have the overlayitem and mapoverlay classes correct? It can be super confusing about which overlay you're adding stuff to – Falmarri Oct 05 '10 at 22:51