ItemizedOverlay.getIndexToDraw 中的 NullPointerException

发布于 2024-08-27 23:35:12 字数 3905 浏览 3 评论 0原文

我有一个相对简单的 MapActivity,我正在尝试显示它 给定地图区域内的“营地”列表。我创建了一个自定义 OverlayItem 的子类称为 CampOverlayItem,是一个自定义的 ItemizedOverlay 称为 CampsOverlay,返回 CampOverlayItems,并且 当然是填充地图的 MapActivity 子类。

我使用 AsyncTask 从数据库中提取覆盖数据 在我的活动中创建。 AsyncTask 是从 ViewTreeObserver.OnGlobalLayoutListener 附加到 MapView。

在AsyncTask的onPostExecute方法中,我创建了一个新实例 我的 CampsOverlay 类并传递给它返回的营地列表 数据库(在 doInBackground 中获取)。然后我调用:

mapView.getOverlays().add(newOverlay);

其中 newOverlay 是我刚刚创建的 CampsOverlay。所有这些代码 运行没有错误,但是当地图尝试绘制自身时,我得到一个 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();
    }

}

有人知道这里会发生什么吗?我尝试过 验证我能控制的一切都是非空的。我可以 如有必要,请提供更多代码。

I have a relatively simple MapActivity that I'm trying to make display
a list of "camps" within a given map region. I've created a custom
subclass of OverlayItem called CampOverlayItem, a custom
ItemizedOverlay called CampsOverlay that returns CampOverlayItems, and
of course a MapActivity subclass that populates the map.

I'm pulling the overlay data from a database using an AsyncTask as
created in my activity. The AsyncTask is triggered from a
ViewTreeObserver.OnGlobalLayoutListener attached to the MapView.

In the onPostExecute method of the AsyncTask, I create a new instance
of my CampsOverlay class and pass it a list of the camps returned from
the database (which are fetched in doInBackground). I then call:

mapView.getOverlays().add(newOverlay);

where newOverlay is the CampsOverlay I just created. All of this code
runs without error, but when the Map tries to draw itself, I get a
NullPointerException with the following stack trace:

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)

Because it seems particularly relevant, here is the code for my
ItemizedOverlay subclass:

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

}

Does anyone have any idea what could be happening here? I've attempted
to verify that everything I have control over is non-null. I can
provide more code if necessary.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

滿滿的愛 2024-09-03 23:35:12

我没有看到您在 CampsOverlay 上调用 populate() 的位置。

这是一个示例项目,显示了覆盖项目的异步加载 - - 如果 populate() 不是问题的话,也许它会给你一些想法。

I do not see where you are calling populate() on your CampsOverlay.

Here is a sample project showing asynchronous loading of overlay items -- perhaps it will give you some ideas, if populate() is not the issue.

枫林﹌晚霞¤ 2024-09-03 23:35:12

他是对的。您必须调用 populate() 来填充覆盖层。您可能希望在构造函数中创建所有覆盖项,调用 populate,然后在 createItem 中仅从列表中返回该项。

He is correct. You must call populate() to populate the overlay. You may want to create all the overlay items in the constructor, call populate, and in createItem just return the item from a list.

ゞ记忆︶ㄣ 2024-09-03 23:35:12

“现在我只需要弄清楚为什么覆盖项目没有出现”

如果您没有,那是因为您必须在 Drawable 上使用静态函数来解释您的标记如何定位。

您可以在您的 CampsOverlay 构造函数中使用它:

super(boundCenter(defaultMarker));

这将表明您的 Drawable 的来源是中心。您还可以使用 boundCenterBottom() 来指示原点是 Drawable 的底部中心。

"Now I just have to figure out why the overlay items aren't appearing"

In case you didn't, it's because you have to use a static function on your Drawable that will explain how is your marker positioned.

You can use this, in your CampsOverlay constructor:

super(boundCenter(defaultMarker));

This will indicate that the origin of your Drawable is the center. You can also use boundCenterBottom() to indicate that the origin is the bottom center of the Drawable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文