所以,我正在尝试使用谷歌地图和firebase制作徒步旅行应用程序。我正在尝试为用户创建的标记编写onChildEventListener支持。它与onChildAdded一起使用 - 它会记录任何新创建的标记并将它们保存在firebase中,然后在地图上显示它们。我在保存onChildRemoved回调的更改之前从db中删除了一些标记,并且当前运行应用程序时我得到一个空指针异常(我假设当它运行onChildAdded回调时它仍然试图通过已经删除的对象)。以下是初始化侦听器的代码:
public void initMarkersListener() {
final DatabaseReference markersRef = FirebaseDatabase
.getInstance()
.getReference()
.child("markers");
markersRef.addChildEventListener(new ChildEventListener() {
public void onChildAdded(DataSnapshot dataSnapshot, String previousKey) {
MarkerData newMarker = dataSnapshot.getValue(MarkerData.class);
if (!huts.containsKey(newMarker)) {
LatLng mark = new LatLng(newMarker.getCoordinates().getLatitude(),
newMarker.getCoordinates().getLongitude());
huts.put(mark, newMarker.getBody());
mMap.addMarker(new MarkerOptions().position(mark).title(huts.get(mark)));
}
}
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
public void onChildRemoved(DataSnapshot dataSnapshot) {
String key = dataSnapshot.getKey();
huts.remove(key);
}
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
如果我将onChildAdded中的if-check更改为(huts.containsKey(newMarker)),那么它不会崩溃,将其保存到数据库中但不会在应用程序中显示。
这是我的数据类:
public class MarkerData {
private MyLatLng coordinates;
private String body;
public MarkerData() {}
public MyLatLng getCoordinates() {
return coordinates;
}
public void setCoordinates(MyLatLng coordinates) {
this.coordinates = coordinates;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
public MarkerData(MyLatLng coords, String body) {
this.coordinates = coords;
this.body = body;
}
以下是firebase db的当前JSON内容:
{
"markers" : {
"-KkjV41j6AmrHjsxuEwc" : {
"body" : "blah",
"coordinates" : {
"latitude" : 47.53951248481877,
"longitude" : 5.2395521476864815
}
},
"-KkkMqnOexWlcCl06gFh" : {
"body" : "Cold"
},
"-KnWNiGnxi8FDHV84VU9" : {
"body" : "canada",
"coordinates" : {
"latitude" : 54.14486310256719,
"longitude" : -103.26476879417896
}
}
},
"routes" : {
"-KmwYdZnXo5C85CyhEih" : {
"encodedPath" : "_}}rB_cj_G~`hxSzvp@",
"rating" : 0
}
}
}
这是堆栈跟踪NPE信息(其中第415行是该行
LatLng mark = new LatLng(newMarker.getCoordinates().getLatitude(),
newMarker.getCoordinates().getLongitude());
):
06-27 06:01:13.013 3706-3706/com.example.alexander.hikebulgaria E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.alexander.hikebulgaria, PID: 3706
java.lang.NullPointerException
at com.example.alexander.hikebulgaria.map.MapsActivity$8.onChildAdded(MapsActivity.java:415)
at com.google.android.gms.internal.zzaip.zza(Unknown Source)
at com.google.android.gms.internal.zzakp.zzcxi(Unknown Source)
at com.google.android.gms.internal.zzaks$1.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5756)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(Native Method)