询问位置许可后,应用程序崩溃

时间:2018-11-05 12:06:08

标签: android crash location

我知道之前也曾问过同样的问题,但是在我的情况下我找不到任何好的答案。基本上,当我单击具有当前位置应用程序的活动时,该应用程序正在停止,然后征求许可。当我允许它时,应用程序将继续正常运行。另一件事是我没有收到错误。

 SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

    mService = Common.getGoogleAPIService();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
    {
        checkLocationPermission();
    }
}

private boolean checkLocationPermission()
{
    if (ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)
    {
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.ACCESS_FINE_LOCATION))
            ActivityCompat.requestPermissions(this,new String[]{

                    Manifest.permission.ACCESS_FINE_LOCATION

            },MY_PERMISSION_CODE);
        else
            ActivityCompat.requestPermissions(this,new String[]{

                    Manifest.permission.ACCESS_FINE_LOCATION

            },MY_PERMISSION_CODE);
        return false;
    }
    else
        return true;
}


@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
    {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)
        {
            buildGoogleApiClien();
            mMap.setMyLocationEnabled(true);
        }
        else
        {
            buildGoogleApiClien();
            mMap.setMyLocationEnabled(true);
        }
    }


}

private synchronized void buildGoogleApiClien()
{
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();
    mGoogleApiClient.connect();
}

@Override
public void onConnected(@Nullable Bundle bundle) {
    mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(1000);
    mLocationRequest.setFastestInterval(1000);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
    if (ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)
    {
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,mLocationRequest,this);
    }
}

@Override
public void onConnectionSuspended(int i) {
    mGoogleApiClient.connect();
}

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

}

@Override
public void onLocationChanged(Location location) {


    mLastLocation = location;
    if (mMarker != null)

        mMarker.remove();

    latitude = location.getLatitude();
    longitude = location.getLongitude();

    LatLng latLng = new LatLng(latitude,longitude);
    MarkerOptions markerOptions = new MarkerOptions()
            .position(latLng);
         //   .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));
  //  mMarker = mMap.addMarker(markerOptions);

1 个答案:

答案 0 :(得分:0)

在AndroidManifest.xml文件中添加位置权限

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />