我创建了一个名为 MapMarker 的简单类,如下所示:
public class MapMarker {
private long id;
private String resName;
public MapMarker(){}
public MapMarker(long id, String resName){
this.id = id;
this.resName = resName;
}
public long getId(){
return id;
}
public String getResName(){
return resName;
}
}
当我在谷歌地图中添加标记时,我还创建了一个 HashMap,以便在单击信息窗口时按顺序创建一个带有一些参数的活动。我正在使用的代码如下,用于添加标记和初始化哈希图
private HashMap<LatLng, MapMarker> mapMarkersData = new HashMap<LatLng, MapMarker>();
markerCoords = new LatLng(lat, lng);
mapMarkersData.put(markerCoords, new MapMarker(markerId, markerRes));
map.addMarker(new MarkerOptions().position(markerCoords).title(title).snippet("Click to see more info!"));
当我尝试从哈希图中获取值时,我得到一个空指针异常。我正在使用的代码如下。
line 360: markerId = mapMarkersData.get(marker.getPosition()).getId();
markerResName = mapMarkersData.get(marker.getPosition()).getResName();
出了什么问题?
编辑:
logcat 如下:
02-23 20:37:55.780: E/AndroidRuntime(4853): FATAL EXCEPTION: main
02-23 20:37:55.780: E/AndroidRuntime(4853): java.lang.NullPointerException
02-23 20:37:55.780: E/AndroidRuntime(4853): at .MapActivity.onInfoWindowClick (MapActivity.java:360)
02-23 20:37:55.780: E/AndroidRuntime(4853): at .GoogleMap$8.h(Unknown Source)
02-23 20:37:55.780: E/AndroidRuntime(4853): at com.google.android.gms.internal.u$a.onTransact(Unknown Source)
02-23 20:37:55.780: E/AndroidRuntime(4853): at android.os.Binder.transact(Binder.java:279)
02-23 20:37:55.780: E/AndroidRuntime(4853): at com.google.android.gms.maps.internal.IOnInfoWindowClickListener$Stub$Proxy.onInfoWindowClick(IOnInfoWindowClickListener.java:82)
02-23 20:37:55.780: E/AndroidRuntime(4853): at maps.y.bw.a(Unknown Source)
02-23 20:37:55.780: E/AndroidRuntime(4853): at maps.a.y.f(Unknown Source)
02-23 20:37:55.780: E/AndroidRuntime(4853): at maps.a.y.a(Unknown Source)
02-23 20:37:55.780: E/AndroidRuntime(4853): at maps.a.bd.c(Unknown Source)
02-23 20:37:55.780: E/AndroidRuntime(4853): at maps.a.dq.onSingleTapConfirmed(Unknown Source)
02-23 20:37:55.780: E/AndroidRuntime(4853): at maps.e.v.onSingleTapConfirmed(Unknown Source)
02-23 20:37:55.780: E/AndroidRuntime(4853): at maps.e.j.handleMessage(Unknown Source)
02-23 20:37:55.780: E/AndroidRuntime(4853): at android.os.Handler.dispatchMessage(Handler.java:99)
02-23 20:37:55.780: E/AndroidRuntime(4853): at android.os.Looper.loop(Looper.java:130)
02-23 20:37:55.780: E/AndroidRuntime(4853): at android.app.ActivityThread.main(ActivityThread.java:3691)
02-23 20:37:55.780: E/AndroidRuntime(4853): at java.lang.reflect.Method.invokeNative(Native Method)
02-23 20:37:55.780: E/AndroidRuntime(4853): at java.lang.reflect.Method.invoke(Method.java:507)
02-23 20:37:55.780: E/AndroidRuntime(4853): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
02-23 20:37:55.780: E/AndroidRuntime(4853): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:670)
02-23 20:37:55.780: E/AndroidRuntime(4853): at dalvik.system.NativeStart.main(Native Method)
抱歉,最后一次编辑已完成。