java.lang.NumberFormatException:无效的int:“android.permission.ACCESS_COARSE_LOCATION”

时间:2016-11-18 06:12:19

标签: android google-maps android-permissions numberformatexception

我正在我的应用中实施Google地图,应用程序因MapsActivity中的以下错误而崩溃。

我在OnMapReady()方法中遇到错误。在MapReady中,我调用了displaySettingsRequest,它显示用户从应用程序中打开位置服务。

地图活动:

public class MapsActivity extends BaseActivity implements OnMapReadyCallback, ConnectionCallbacks, OnConnectionFailedListener {    
private void displayLocationSettingsRequest(Context context) {
    GoogleApiClient googleApiClient = new GoogleApiClient.Builder(context)

    PendingResult<LocationSettingsResult> result = LocationServices.SettingsApi.checkLocationSettings(googleApiClient, builder.build());
    result.setResultCallback(result1 -> {
        final Status status = result1.getStatus();
        switch (status.getStatusCode()) {
            case LocationSettingsStatusCodes.SUCCESS:
                Log.i(TAG, "All location settings are satisfied.");
                break;
            case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                Log.i(TAG, "Location settings are not satisfied. Show the user a dialog to upgrade location settings ");

                try {
                    // Show the dialog by calling startResolutionForResult(), and check the result
                    // in onActivityResult().
                    status.startResolutionForResult(MapsActivity.this, HawkConstants.REQUEST_CODE);
                } catch (IntentSender.SendIntentException e) {
                    Log.i(TAG, "PendingIntent unable to execute request.");
                }
                break;
            case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                Log.i(TAG, "Location settings are inadequate, and cannot be fixed here. Dialog not created.");
                break;
        }
    });
}

@Override
public void onMapReady(GoogleMap googleMap) {
    displayLocationSettingsRequest(App.getContext());
    mMap = googleMap;
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {            
        return;
    }
    mMap.setMyLocationEnabled(true);

}

@Override
public void onConnected(@Nullable Bundle bundle) {
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {          
        ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_COARSE_LOCATION},
                Integer.parseInt(Manifest.permission.ACCESS_COARSE_LOCATION));
        return;
    }
    mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
            mGoogleApiClient);
}

1 个答案:

答案 0 :(得分:1)

你正试图:

Integer.parseInt(Manifest.permission.ACCESS_COARSE_LOCATION));

Manifest.permission.ACCESS_COARSE_LOCATION不是十进制系统中数字的String表示。它会导致NumberFormatException

你StackTrace说,Manifest.permission.ACCESS_COARSE_LOCATION“android.permission.ACCESS_COARSE_LOCATION”。您只能解析String"10""1234123""-11121",但Java不会将"java cute string"解析为数字。

您可以详细了解NFE here