我想知道是否有人可以帮助我,我想在ConcurrentHashMap中保存对新创建的Marker的引用,我似乎没有问题存储引用但是当我尝试检索它时应用程序崩溃并抛出一个NullPointer Exception,下面是我存储和检索标记的方法。
将其存储在mMarkers
private Map<LatLng, Marker> mMarkers = new ConcurrentHashMap<LatLng, Marker>();
LatLng currPos = new LatLng(map.getCameraPosition().target.latitude,map.getCameraPosition().target.longitude);
Marker marker = map.addMarker(new MarkerOptions()
.position(currPos)
);
mMarkers.put(currPos, marker);
locationCount++;
SharedPreferences.Editor editor = settings.edit();
editor.putString("mlat"+ Integer.toString((locationCount-1)), Double.toString(marker.getPosition().latitude));
editor.putString("mlng"+ Integer.toString((locationCount-1)), Double.toString(marker.getPosition().longitude));
editor.putInt("locationCount", locationCount);
editor.putString("mzoom", Float.toString(map.getCameraPosition().zoom));
editor.commit();
将其删除失败
map.setOnMapLongClickListener(new OnMapLongClickListener() {
@Override
public void onMapLongClick(LatLng point) {
Marker marker = mMarkers.get(point);
marker.remove(); //fails here
mMarkers.remove(point);
}
});
我使用标记LatLng位置作为存储它的键。
04-06 16:52:05.535: E/AndroidRuntime(26601): FATAL EXCEPTION: main
04-06 16:52:05.535: E/AndroidRuntime(26601): java.lang.NullPointerException
04-06 16:52:05.535: E/AndroidRuntime(26601): at com.grim.pipboid.FragmentMap$1.onMapLongClick(FragmentMap.java:154)
04-06 16:52:05.535: E/AndroidRuntime(26601): at com.google.android.gms.maps.GoogleMap$7.onMapLongClick(Unknown Source)
04-06 16:52:05.535: E/AndroidRuntime(26601): at com.google.android.gms.maps.internal.j$a.onTransact(Unknown Source)
04-06 16:52:05.535: E/AndroidRuntime(26601): at android.os.Binder.transact(Binder.java:310)
04-06 16:52:05.535: E/AndroidRuntime(26601): at euq.a(SourceFile:93)
04-06 16:52:05.535: E/AndroidRuntime(26601): at maps.c.j.b(Unknown Source)
04-06 16:52:05.535: E/AndroidRuntime(26601): at maps.ay.an.c(Unknown Source)
04-06 16:52:05.535: E/AndroidRuntime(26601): at maps.ay.bc.onLongPress(Unknown Source)
04-06 16:52:05.535: E/AndroidRuntime(26601): at maps.bo.g.onLongPress(Unknown Source)
04-06 16:52:05.535: E/AndroidRuntime(26601): at maps.bo.h.c(Unknown Source)
04-06 16:52:05.535: E/AndroidRuntime(26601): at maps.bo.i.handleMessage(Unknown Source)
04-06 16:52:05.535: E/AndroidRuntime(26601): at android.os.Handler.dispatchMessage(Handler.java:99)
04-06 16:52:05.535: E/AndroidRuntime(26601): at android.os.Looper.loop(Looper.java:137)
04-06 16:52:05.535: E/AndroidRuntime(26601): at android.app.ActivityThread.main(ActivityThread.java:5227)
04-06 16:52:05.535: E/AndroidRuntime(26601): at java.lang.reflect.Method.invokeNative(Native Method)
04-06 16:52:05.535: E/AndroidRuntime(26601): at java.lang.reflect.Method.invoke(Method.java:511)
04-06 16:52:05.535: E/AndroidRuntime(26601): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
04-06 16:52:05.535: E/AndroidRuntime(26601): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
04-06 16:52:05.535: E/AndroidRuntime(26601): at dalvik.system.NativeStart.main(Native Method)
感谢您阅读
答案 0 :(得分:0)
用户可以长按地图上的任意位置。它不必处于现有标记的精确纬度和经度。因此,LatLng
中不存在HashMap
。