Android GeoFire项目上的位置NullPointerException错误
刚刚习惯了Java和Android,我就犯了上面的错误 当我点击应用程序上的一个按钮来查找“responder”的用户类型时,它就会被触发。应用程序崩溃,我收到以下错误:Android GeoFire项目上的位置NullPointerException错误,android,firebase,nullpointerexception,android-location,geofire,Android,Firebase,Nullpointerexception,Android Location,Geofire,刚刚习惯了Java和Android,我就犯了上面的错误 当我点击应用程序上的一个按钮来查找“responder”的用户类型时,它就会被触发。应用程序崩溃,我收到以下错误: java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLatitude()' on a null object reference
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.setLocation(userID, new GeoLocation(mLastLocation.getLatitude(), mLastLocation.getLongitude()));
setLocation(userID,新地理位置(mLastLocation.getLatitude(),mLastLocation.getLongitude());
它显示数据库中的空值,然后将显示以下错误
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)
java.lang.NullPointerException:尝试对空对象引用调用虚拟方法“double android.location.location.getLatitude()”
在com.jamesboyle.firsthelp.PatientMapActivity$2.onClick(PatientMapActivity.java:83)
因此,数据库位置子级必须具有位置值语句
geoFire.setLocation(userID,new GeoLocation(mLastLocation.getLatitude(),mLastLocation.getLongitude())中可能重复的代码>来自评论:嗨,虽然链接是分享知识的好方法,但如果将来它们被破坏了,它们不会真正回答这个问题。在回答中添加回答问题的链接的基本内容。如果内容太复杂或太大而不适合此处,请描述建议解决方案的总体思路。请记住始终保留原始解决方案网站的链接引用。见: