【问题标题】:Adding Points to Polygon from an Array从数组向多边形添加点
【发布时间】:2013-07-26 22:36:13
【问题描述】:

当我尝试从数组中向 Google 地图多边形添加点时,出现空指针异常。

if(inPolygon.check(list[i].vertices, list[i].latCoords, list[i].longCoords, latitude, longitude) != true) {

    PolygonOptions rectOptions = new PolygonOptions();
                for(int n = 0; n <= list[i].vertices; n++){
                    rectOptions.add(new LatLng(list[i].latCoords[n], list[i].longCoords[n]));
                }
}

我有一个对象数组(列表),每个对象有两个数组:一个用于纬度坐标(latCoords),一个用于经度坐标(longCoords)。我想通过迭代它们来从两个数组的坐标创建一个多边形,就像你在 for 循环中看到的那样。

但是当我尝试这样做时,我不断收到一个空指针异常,我不知道为什么。对于这个问题,我很感激任何帮助或建议,因为我只是 Maps/Android 开发的初学者。

【问题讨论】:

  • 哪里发生了异常,你确定list是非空的并且只包含非空引用吗?
  • LogCat 指向将点添加到多边形 (116) 的线。我相信列表是非空的,因为它包含两个对象。

标签: java android arrays google-maps


【解决方案1】:

怀疑这是问题所在:

for(int n = 0; n <= list[i].vertices; n++)

你在这里使用n

list[i].latCoords[n]

(与longCoords 相同)- 所以当n 等于list[i].vertices 时,这可能是一个空引用。

我怀疑你希望你的循环检查严格小于(这更常见):

for (int n = 0; n < list[i].vertices; n++)

【讨论】:

  • 嗯.. 当我这样做时,我得到一个不同的错误。 “IndexOutofBoundsException:无效索引 0,大小为 0”。
  • @RowanK.:嗯,我怀疑你在不同的地方得到了这个,这就是进步。所以接下来你需要看看发生在哪里,并解决它。
  • 它说我在第 121 行遇到了这个错误,这是我创建多边形 Polygon polygon = googleMap.addPolygon(rectOptions);
  • @RowanK.: 好吧,您甚至没有向我们展示该代码...您应该尝试自己诊断它,如果您无法找出问题所在,请询问 new 带有所有相关代码的问题。
猜你喜欢
  • 2011-08-18
  • 1970-01-01
  • 2013-02-05
  • 2014-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-31
相关资源
最近更新 更多