Clean Code That Works
Header
Clean Code That Works
Header
Clean Code That Works
Header
Clean Code That Works
Header
Clean Code That Works
Header
Clean Code That Works
Header
Clean Code That Works
Header

Android: NullPointerException when scrolling through a MapView w/ ItemizedOverlay

November 13th, 2010 | Posted by Jay in Computing

When I implemented a MapView with an ItemizedOverlay (the implementation steps are described in the Android developer documentation), I encountered a flaw in the API, or at least in the corresponding documentation. If you add an ItemizedOverlay to your MapView, but do not add any OverlayItems (which may be the default for your application, with OverlayItems being added during runtime), scrolling through the MapView causes the following NullPointerException:

ERROR/AndroidRuntime(232): java.lang.NullPointerException
  at com.google.android.maps.ItemizedOverlay.getItemsAtLocation(ItemizedOverlay.java:617)
  at com.google.android.maps.ItemizedOverlay.getItemAtLocation(ItemizedOverlay.java:586)
  at com.google.android.maps.ItemizedOverlay.handleMotionEvent(ItemizedOverlay.java:498)
  at com.google.android.maps.ItemizedOverlay.onTouchEvent(ItemizedOverlay.java:572)
  at com.google.android.maps.OverlayBundle.onTouchEvent(OverlayBundle.java:63)
  at com.google.android.maps.MapView.onTouchEvent(MapView.java:625)
  at android.view.View.dispatchTouchEvent(View.java:3709)
  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:852)
  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:822)
  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:822)
  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:822)
  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
  at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
  at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:822)
  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:822)
  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:822)
  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:822)
  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:822)
  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:822)
  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
  at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
  at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
  at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
  at android.os.Handler.dispatchMessage(Handler.java:99)
  at android.os.Looper.loop(Looper.java:123)
  at android.app.ActivityThread.main(ActivityThread.java:4363)
  at java.lang.reflect.Method.invokeNative(Native Method)
  at java.lang.reflect.Method.invoke(Method.java:521)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
  at dalvik.system.NativeStart.main(Native Method)

After long hours of debugging, I was able to find help in the Android Bug Tracking system. An easy workaround for this problem (which should really be addressed in the API and/or the corresponding documentation) is to add a call to the “populate()” function in the constructor of the class that extends ItemizedOverlay. Then, your subclass constructor should look like this:

super(boundCenterBottom(defaultMarker));
populate();

This solves the problem and scrolling, even without any OverlayItems, works like a charm.

You can follow any responses to this entry through the RSS 2.0 You can leave a response, or trackback.

7 Responses



Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">