-4

I am implementing Google map in my app and the app crashed with the below error in MapsActivity.

I'm getting the error in the OnMapReady() method. In MapReady i have called the displaySettingsRequest that shows the user to turn on the Location services from within the app.

Maps Activity:

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);
}
Durga
  • 73
  • 12
  • show us code. about that `MapsActivity` – AJay Nov 18 '16 at 06:14
  • hi @AJay i have edited my question with code, please have a look – Durga Nov 18 '16 at 07:42
  • this is issue with your imports check import for `Manifest` there should be only one import line for that. – AJay Nov 18 '16 at 08:02
  • Sorry, actually my mistake I have set target SDK to 23 and included the permission check request but didn't handle the permission check. The permission to access location has been denied and when the app is relaunched it crashed. – Durga Nov 18 '16 at 08:33

1 Answers1

1

You're trying to:

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

but Manifest.permission.ACCESS_COARSE_LOCATION isn't a String representation of a number in decimal system. It causes NumberFormatException.

You StackTrace says, that the Manifest.permission.ACCESS_COARSE_LOCATION is “android.permission.ACCESS_COARSE_LOCATION”. You can only parse Strings like "10", "1234123", "-11121", however Java won't parse "java cute string" to a number.

You can read more about NFE here.

Community
  • 1
  • 1
xenteros
  • 15,586
  • 12
  • 56
  • 91
  • thank you for helping, and yeah the exception in StackTrace says what you had clearly explained but the root cause was my mistake that I did not handle the permission model properly. I missed to handle, after the user denies the location permission. – Durga Nov 22 '16 at 09:02