我正在尝试使用geoFire
获取位置,并将标记放置在该位置的地图上,但是geoFire.getLocation()
没有运行。我尝试放置一些检查日志以查看代码出了问题的地方,并发现在运行日志OKAY 1
和Log键之后,代码直接跳转到日志OKAY 3
。这是我的logcat的结果:
/supplier.watersystem.com.suppliersideapp I / OKAY:好的1 /supplier.watersystem.com.suppliersideapp I / KEY找到:station_0 /supplier.watersystem.com.suppliersideapp I / OKAY:好的3
11-03 19:10:25.966 32678-32678 / supplier.watersystem.com.suppliersideapp E / AndroidRuntime:FATAL EXCEPTION:main 流程:supplier.watersystem.com.suppliersideapp,PID:32678
即使我得到的ID是正确的。我在放置标记的地方收到了NullPointerException异常,因为我的位置ArrayList将为空,它必须具有一定的值,因为如果在获取位置时出错,则必须将其记录下来,否则必须从数据库中放置该位置,或者放置0,0 LatLng 。
这是我的代码:
//reading station data from database and adding marker
mDatabase = FirebaseDatabase.getInstance().getReference().child("STATIONS");
ValueEventListener stationListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
int i=0;
DatabaseReference geoFireRef = FirebaseDatabase.getInstance().getReference().child("STATION_LOCATIONS");
GeoFire geoFire = new GeoFire(geoFireRef);
for(DataSnapshot stationsSnapshot: dataSnapshot.getChildren()){
stations.add(stationsSnapshot.getValue(Station.class));
//Fetching Location
Log.i("OKAY ", "Okay 1");
Log.i("KEY FOUND ", stationsSnapshot.getKey());
geoFire.getLocation(stationsSnapshot.getKey(), new LocationCallback() {
@Override
public void onLocationResult(String key, GeoLocation location) {
Log.i("OKAY ", "Okay 2");
if(location!=null){
Log.i("OKAY ", "Okay 2.1 not null");
stationLocations.add(new LatLng(location.latitude,location.longitude));
}
else{
Log.i("OKAY ", "Okay 2.2 null");
stationLocations.add(new LatLng(0,0));
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.i("Location Read [ERROR] ",databaseError.getMessage());
}
});
//placing marker
Log.i("OKAY ", "Okay 3");
mMap.addMarker(new MarkerOptions().position(stationLocations.get(i)).title(stations.get(i).getName()));
Log.i("OKAY ", "Okay 4");
i++;
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.i("Station Read [ERROR]",databaseError.getMessage());
}
};
mDatabase.addListenerForSingleValueEvent(stationListener);
答案 0 :(得分:1)
我认为它不起作用,因为您的okay2部分是异步的,并且它没有足够的时间来获取答案,因为okay3部分没有等待它。