2015-04-04 102 views
0

使用Google Maps API for Android(v2),我试图将一些国家的Polygon近似值存储到数据结构中,特别是以String作为键的地图(国家/地区名称)和PolygonOptions的ArrayList。将元素添加到字符串地图,Arraylist

该变种是私人和宣称:

private Map<String, ArrayList<PolygonOptions>> worldMap; 

但我的代码崩溃,当我试图用PolygonOption来填补它,下面的功能:

private void addSomeCountry() { 
     ArrayList <PolygonOptions> MyPoli = new ArrayList<PolygonOptions>(); 
     PolygonOptions rectOptions = new PolygonOptions() 
       .add(new LatLng(42.569962, 1.78172)) 
       .add(new LatLng(42.509438, 1.723611)) 
       .add(new LatLng(42.601944, 1.445833)) 
       .add(new LatLng(42.569962, 1.78172)) 
       .fillColor(Color.TRANSPARENT) 
       .strokeColor(Color.CYAN); 
     MyPoli.add(rectOptions); 
     worldMap.put("Andorra", MyPoli); 
    } 

具体来说,它崩溃出现以下错误:

04-04 21:20:30.335 22562-22562/otorrillas.geography E/AndroidRuntime﹕ FATAL EXCEPTION: main 
    Process: otorrillas.geography, PID: 22562 
Caused by: java.lang.NullPointerException 
      at otorrillas.geography.DataSaver.addSomeCountry(DataSaver.java:49) 
      at otorrillas.geography.DataSaver.onCreate(DataSaver.java:32) 

我做错了什么?

回答

2

我没有看到您在任何地方初始化worldMap

private Map<String, ArrayList<PolygonOptions>> worldMap = new HashMap<>(); 

应该做的。

+0

是的,它的确如此。谢谢 :) – otorrillas 2015-04-04 19:49:52