Android:无法停止活动(APPCRASH)

时间:2018-02-04 07:19:29

标签: java android firebase firebase-authentication

我是Android新手,我正在开发一个使用Firebase和GoogleMaps API的Android项目,在我的DriverMapsActivity中我有一个注销按钮,当我点击退出按钮时,应用程序崩溃并出现错误

  

" java.lang.RuntimeException:无法停止活动。"

如果有人能指出我做错了什么,我真的很感激

代码:

Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            position = bundle.getString("pos1");
}

        bus3_txt_2.setText(position2);

logcat的:

public class DriverMapsActivity extends FragmentActivity implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, com.google.android.gms.location.LocationListener {

private GoogleMap mMap;
GoogleApiClient googleApiClient;
Location lastLocation;
LocationRequest locationRequest;
Button btnLogout;

FirebaseAuth mAuth;
FirebaseUser currentUser;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_driver_maps);

    mAuth = FirebaseAuth.getInstance();
    currentUser = mAuth.getCurrentUser();

    btnLogout = (Button) findViewById(R.id.btnLogout);

    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

    btnLogout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mAuth.signOut();
            Intent intent = new Intent(DriverMapsActivity.this, MainActivity.class);
            startActivity(intent);
            finish();
            return;
        }
    });

}

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    buildGoogleApiClient();
    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return;
    }
    mMap.setMyLocationEnabled(true);
    MapStyleOptions styleOptions =  MapStyleOptions.loadRawResourceStyle(this, R.raw.google_maps);
    mMap.setMapStyle(styleOptions);
}

@Override
public void onConnected(@Nullable Bundle bundle) {
    locationRequest = new LocationRequest();
    locationRequest.setInterval(1000);
    locationRequest.setFastestInterval(1000);
    locationRequest.setPriority(locationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

        return;
    }
    LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, this);
}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

}

@Override
public void onLocationChanged(Location location) {
    lastLocation = location;
    LatLng latLang = new LatLng(location.getLatitude(), location.getLongitude());
    mMap.moveCamera(CameraUpdateFactory.newLatLng(latLang));
    //mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLang, 15),5000,null);
    mMap.animateCamera(CameraUpdateFactory.zoomTo(15));
    String userId = mAuth.getCurrentUser().getUid();
    DatabaseReference db = FirebaseDatabase.getInstance().getReference().child("driversAvailable");
    GeoFire geoFire = new GeoFire(db);
    geoFire.setLocation(userId, new GeoLocation(location.getLatitude(), location.getLongitude()));
}

protected synchronized void buildGoogleApiClient()
{
    googleApiClient = new GoogleApiClient.Builder(this).addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this).addApi(LocationServices.API).build();

    googleApiClient.connect();
}

@Override
protected void onStop() {
    String userId = FirebaseAuth.getInstance().getCurrentUser().getUid();
    DatabaseReference db =       FirebaseDatabase.getInstance().getReference().child("driversAvailable");
    GeoFire geoFire = new GeoFire(db);
    geoFire.removeLocation(userId);
    super.onStop();
    googleApiClient.disconnect();
   }
} 

1 个答案:

答案 0 :(得分:1)

错误消息很清楚,在你的onStop()中,FirebaseAuth.getInstance().getCurrentUser()返回null(缺少登录用户),但你仍然在那个空的上调用getUid()值。不要在null对象上调用方法。