【问题标题】:Android : Unable to stop activity (APPCRASH)Android:无法停止活动(APPCRASH)
【发布时间】:2018-02-04 07:19:29
【问题描述】:

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

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

如果有人能指出我做错了什么,我将不胜感激

代码:

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();
   }
} 

Logcat:

        Process: com.shreyasbangera.citycab, PID: 22709
    java.lang.RuntimeException: Unable to stop activity {com.shreyasbangera.citycab/com.shreyasbangera.citycab.DriverMapsActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.firebase.auth.FirebaseUser.getUid()' on a null object reference
    at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:4632)
    at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:4697)
    at android.app.ActivityThread.-wrap7(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1720)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6816)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1563)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1451)
    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.firebase.auth.FirebaseUser.getUid()' on a null object reference
    at com.shreyasbangera.citycab.DriverMapsActivity.onStop(DriverMapsActivity.java:132)
    at android.app.Instrumentation.callActivityOnStop(Instrumentation.java:1297)
    at android.app.Activity.performStop(Activity.java:7228)
    at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:4627)
    at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:4697) 
    at android.app.ActivityThread.-wrap7(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1720) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:154) 
    at android.app.ActivityThread.main(ActivityThread.java:6816) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1563) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1451) 

【问题讨论】:

    标签: java android firebase firebase-authentication


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2013-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多