我尝试显示一个相对简单的 MapActivity 给定 map 区域内的“营地”列表。我创建了一个自定义 OverlayItem 的子类称为 CampOverlayItem,一个自定义 ItemizedOverlay 称为 CampsOverlay,它返回 CampOverlayItems,并且 当然是填充 map 的 MapActivity 子类。
我正在使用 AsyncTask 从数据库中提取覆盖数据 在我的 Activity 中创建。 AsyncTask 是从一个 附加到 MapView 的 ViewTreeObserver.OnGlobalLayoutListener。
在 AsyncTask 的 onPostExecute 方法中,我创建了一个新实例 我的 CampsOverlay 类并将返回的营地列表传递给它 数据库(在 doInBackground 中获取)。然后我打电话:
mapView.getOverlays().add(newOverlay);
其中 newOverlay 是我刚刚创建的 CampsOverlay。所有这些代码 运行没有错误,但是当 Map 尝试自行绘制时,我得到一个 具有以下堆栈跟踪的 NullPointerException:
java.lang.NullPointerException
at
com.google.android.maps.ItemizedOverlay.getIndexToDraw(ItemizedOverlay.java:
211)
at
com.google.android.maps.ItemizedOverlay.draw(ItemizedOverlay.java:240)
at com.google.android.maps.Overlay.draw(Overlay.java:179)
at com.google.android.maps.OverlayBundle.draw(OverlayBundle.java:
42)
at com.google.android.maps.MapView.onDraw(MapView.java:476)
at android.view.View.draw(View.java:6274)
at android.view.ViewGroup.drawChild(ViewGroup.java:1526)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1256)
at android.view.ViewGroup.drawChild(ViewGroup.java:1524)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1256)
at android.view.View.draw(View.java:6277)
at android.widget.FrameLayout.draw(FrameLayout.java:352)
at android.view.ViewGroup.drawChild(ViewGroup.java:1526)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1256)
at android.view.ViewGroup.drawChild(ViewGroup.java:1524)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1256)
at android.view.ViewGroup.drawChild(ViewGroup.java:1524)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1256)
at android.view.ViewGroup.drawChild(ViewGroup.java:1524)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1256)
at android.view.ViewGroup.drawChild(ViewGroup.java:1524)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1256)
at android.view.View.draw(View.java:6277)
at android.widget.FrameLayout.draw(FrameLayout.java:352)
at android.view.ViewGroup.drawChild(ViewGroup.java:1526)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1256)
at android.view.View.draw(View.java:6277)
at android.widget.FrameLayout.draw(FrameLayout.java:352)
at com.android.internal.policy.impl.PhoneWindow
$DecorView.draw(PhoneWindow.java:1883)
at android.view.ViewRoot.draw(ViewRoot.java:1332)
at android.view.ViewRoot.performTraversals(ViewRoot.java:1097)
at android.view.ViewRoot.handleMessage(ViewRoot.java:1613)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4203)
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:791)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
at dalvik.system.NativeStart.main(Native Method)
因为它看起来特别相关,所以这是我的代码 ItemizedOverlay 子类:
public class CampsOverlay extends ItemizedOverlay<CampOverlayItem> {
private ArrayList<Camp> camps = null;
public CampsOverlay(Drawable defaultMarker, ArrayList<Camp> theCamps)
{
super(defaultMarker);
this.camps = theCamps;
}
@Override
protected CampOverlayItem createItem(int i) {
Camp camp = camps.get(i);
CampOverlayItem item = new CampOverlayItem(camp);
return item;
}
@Override
protected boolean onTap(int index) {
// TODO Auto-generated method stub
return super.onTap(index);
}
@Override
public int size() {
return camps.size();
}
}
有人知道这里会发生什么吗?我试过 验证我控制的所有内容都是非空的。我可以 如有必要,请提供更多代码。
最佳答案
我没有看到您在 CampsOverlay
上调用 populate()
的位置。
Here is a sample project显示覆盖项的异步加载 - 如果 populate()
不是问题,它可能会给您一些想法。
关于android - ItemizedOverlay.getIndexToDraw 中的 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2553410/