我在我的应用中使用经过身份验证的用户的连续位置更新。当我从我的应用程序注销时,它崩溃了。我意识到应用程序因位置更新而崩溃,因此如果没有经过身份验证的用户,我会考虑使用locmngr.removeUpdates(this)
删除位置更新。当用户点击logout mAath.getUid()
时,它变为空,我得到的错误是Attempt to invoke virtual method 'boolean java.lang.String.isEmpty()' on a null object reference
。
try {
locmngr.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 7, 30, new LocationListener() {
@Override
public void onLocationChanged(final Location location) {
// Add a marker at your current location and move the camera
Log.e("location", location.toString());
Log.e("location", "updating");
// Toast.makeText(ProfileNavigation.this, "Location : "+location.getLatitude()+" , "+location.getLongitude(), Toast.LENGTH_SHORT).show();
//double a = location.getLongitude();
//double b = location.getLatitude();
if(!mAuth.getUid().isEmpty()) {
db.collection("UserProfile").document(mAuth.getUid()).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot documentSnapshot = task.getResult();
if (documentSnapshot.exists()) {
Log.e("maps", "updating");
Map<String, Object> data = new HashMap<>();
data.put("Latitude", location.getLatitude());
data.put("Longitude", location.getLongitude());
db.collection("UserProfile").document(mAuth.getUid())
.set(data, SetOptions.merge())
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Log.e("maps", "DocumentSnapshot successfully written!");
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.e("maps", "Error writing document", e);
}
});
} else {
Log.e("firestore", "Error getting documents.", task.getException());
}
} else {
Log.e("firestore", "Error getting documents.", task.getException());
}
}
});
}
else {
locmngr.removeUpdates(this);
}
}
}
}
答案 0 :(得分:1)
在removeUpdates
方法中调用onPause()
方法。
@Override
protected void onPause() {
super.onPause();
locmngr.removeUpdates(this);
Log.i(TAG, "onPause, done");
}
你也得到了:
尝试调用虚方法'boolean java.lang.String.isEmpty()
if(logout mAath.getUid()!=null){
//do operation here
}
希望它能帮到你!!