刚刚适应Java和Android,并且遇到了以上错误。
[当我在应用程序上单击一个按钮,该按钮将寻找用户类型的“响应者”时触发。应用程序崩溃了,我得到了这个错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLatitude()' on a null object reference
at com.jamesboyle.firsthelp.PatientMapActivity$2.onClick(PatientMapActivity.java:83)
我不确定此刻什么是空对象
mRequest.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String userID = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("patientRequest");
GeoFire geoFire = new GeoFire(ref);
geoFire.setLocation(userID, new GeoLocation(mLastLocation.getLatitude(), mLastLocation.getLongitude()));
pickupLocation = new LatLng(mLastLocation.getLatitude(), mLastLocation.getLongitude());
mMap.addMarker(new MarkerOptions().position(pickupLocation).title("Respond Now"));
mRequest.setText("Locating Responder...");
getClosestResponder();
}
});
}
private int radius =1;
private Boolean responderFound = false;
private String responderFoundID;
private void getClosestResponder(){
DatabaseReference responderLocation = FirebaseDatabase.getInstance().getReference().child("respondersAvailable");
GeoFire geoFire = new GeoFire(responderLocation);
GeoQuery geoQuery = geoFire.queryAtLocation(new GeoLocation(pickupLocation.latitude, pickupLocation.longitude), radius);
geoQuery.removeAllListeners();
geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
@Override
public void onKeyEntered(String key, GeoLocation location) {
if (!responderFound) {
responderFound = true;
responderFoundID = key;
}
}
替换
pickupLocation = new LatLng(mLastLocation.getLatitude(),mLastLocation.getLongitude());` with `geoFire.setLocation("firebase-hq", new GeoLocation(37.7853889, -122.4056973));
有关详细信息和理解,请访问GeoFire for Java — Realtime location queries with Firebase