我已经编写了一些代码来从geoQuery和GeoFencing获取通知,但是在某些时候我遇到了一些例外。 我尝试了很多方法来摆脱这种情况,但一切都失败了。 这是我得到空指针异常的代码
GeoQuery geoQuery = geoFire.queryAtLocation(new GeoLocation(start_Point.latitude, start_Point.longitude), 0.05f);
geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
@Override
public void onKeyEntered(String key, GeoLocation location) {
SendNotification("Child", String.format("%s reached to school", key));
}
@Override
public void onKeyExited(String key) {
SendNotification("Child", String.format("%s went out from school", key));
}
@Override
public void onKeyMoved(String key, GeoLocation location) {
Log.d("MOVE", String.format("%s Child is on his Way [%f / %f]", key, location.latitude, location.longitude) );
}
@Override
public void onGeoQueryReady() {
}
@Override
public void onGeoQueryError(DatabaseError error) {
Log.e("Error" , ""+ error);
}
});
GeoQuery endPoint = geoFire.queryAtLocation(new GeoLocation(31.8020471, 74.2594809), 0.05f);
endPoint.addGeoQueryEventListener(new GeoQueryEventListener() {
@Override
public void onKeyEntered(String key, GeoLocation location) {
SendNotification("Child", String.format("%s reached to home", key));
}
@Override
public void onKeyExited(String key) {
SendNotification("Child", String.format("%s went out from home", key));
}
@Override
public void onKeyMoved(String key, GeoLocation location) {
}
@Override
public void onGeoQueryReady() {
}
@Override
public void onGeoQueryError(DatabaseError error) {
}
});
这是我的LogCat
Process: com.softtech.aqeel.childsequrity, PID: 4167
java.lang.NullPointerException: Attempt to invoke virtual method 'com.firebase.geofire.GeoQuery com.firebase.geofire.GeoFire.queryAtLocation(com.firebase.geofire.GeoLocation, double)' on a null object reference
at com.softtech.aqeel.childsequrity.GoogleMaps$3.Map(GoogleMaps.java:281)
at com.softtech.aqeel.childsequrity.GoogleMaps$3.run(GoogleMaps.java:233)
at com.softtech.aqeel.childsequrity.GoogleMaps.startRepeating(GoogleMaps.java:225)
at com.softtech.aqeel.childsequrity.GoogleMaps.onMapReady(GoogleMaps.java:152)
at com.google.android.gms.maps.zzak.zza(Unknown Source:7)
at com.google.android.gms.maps.internal.zzaq.dispatchTransaction(Unknown Source:31)
at com.google.android.gms.internal.maps.zzb.onTransact(Unknown Source:22)
at android.os.Binder.transact(Binder.java:604)