用户注销时应用程序崩溃



伙计们,我一直在制作这个简单的优步应用程序,每当Driver LogsOut我的应用程序因getCurrentUser()方法返回null值而崩溃时。但是,每当用户logsOut正在关闭活动,并且在调用getCurrentUser()之前,我都有这个if(getApplicationContext()!=null),它应该返回false,因为活动已经关闭,所以它应该阻止调用getCurrentUser()。如何防止它崩溃?我在这里错过了什么?

这就是点击按钮我注销我的用户的地方

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//some code here
mLogout=(Button)findViewById(R.id.logout);
mLogout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
isLoggingOut = true;
disconnectDriver();

//Signing out user here
FirebaseAuth.getInstance().signOut();
Intent intent = new Intent(DriverMapActivity.this, MainActivity.class);
startActivity(intent);
return;
}
});
//some code here
}

这就是错误发生的地方

@Override
//this is going to run whenever the driver location changes
public void onLocationChanged(Location location) {
if(getApplicationContext()!=null) {
mLastLocation = location;
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());

mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.zoomTo(11));
//
//This is where the error is happening
//java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.firebase.auth.FirebaseUser.getUid()' on a null object reference

String userId = FirebaseAuth.getInstance().getCurrentUser().getUid();

DatabaseReference refAvailable = FirebaseDatabase.getInstance().getReference("driversAvailable");

DatabaseReference refWorking  = FirebaseDatabase.getInstance().getReference("driversWorking");

GeoFire geoFireAvailable = new GeoFire(refAvailable);

GeoFire geoFireWorking = new GeoFire(refWorking);

switch(customerId){

case "":
geoFireWorking.removeLocation(userId);
geoFireAvailable.setLocation(userId, new GeoLocation(location.getLatitude(), location.getLongitude()));
break;
default:

geoFireAvailable.removeLocation(userId);
geoFireWorking.setLocation(userId, new GeoLocation(location.getLatitude(), location.getLongitude()));
break;
}
}
}

这是错误日志

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.jonanako.uber, PID: 15522
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.firebase.auth.FirebaseUser.getUid()' on a null object reference
at com.jonanako.uber.DriverMapActivity.onLocationChanged(DriverMapActivity.java:211)
at com.google.android.gms.internal.location.zzat.notifyListener(com.google.android.gms:play-services-location@@18.0.0:2)
at com.google.android.gms.common.api.internal.ListenerHolder.zaa(com.google.android.gms:play-services-base@@18.0.1:2)
at com.google.android.gms.common.api.internal.zacb.run(Unknown Source:4)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

由于您在每次单击按钮时都会将false分配给isLoggingOut = true;,因此您应该只检查if(getApplicationContext()!=null && !isLoggingOut),这样代码就不会在isLoggingOut==false;时运行

getApplicationContext()finish()Activity时不会是null,即使是最后一个或唯一一个。考虑使用if(!isFinishing())、生命周期检查或仅设置自己的boolean标志。

此外,您还可以(应该(检查getCurrentUser()是否返回null,这意味着用户将注销。如果是null,则不要对返回的对象调用任何方法(因为实际上什么都不返回(

if(!isFinishing() && FirebaseAuth.getInstance().getCurrentUser()!=null) {

最新更新