I am developing an application that allows to retrieve the current location address using Geocoder. this is the step to get location adresse that i follow :
check if GPS is enable on device
if it is request Location permissionspermissions else if GPS is disable on device request enable it than request permission
initialise Geocoder and get adresse latitude and logitude the get Adresse
if the GPS is Enable before clicking on button to get Location EveryThing work fine . but the problem is when the GPS is disable whene clicking on button to get Location even if the app request to enable it and it will be enable i get this error message java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLatitude()' on a null object reference
code that i used
Onclick Methode (test if GPS enabled than call getLocation Methode)
locationButton.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public void onClick(View view) {
//Check permissions
locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(5000);
locationRequest.setFastestInterval(2000);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
.addLocationRequest(locationRequest);
builder.setAlwaysShow(true);
Task<LocationSettingsResponse> result = LocationServices.getSettingsClient(getApplicationContext())
.checkLocationSettings(builder.build());
result.addOnCompleteListener(new OnCompleteListener<LocationSettingsResponse>() {
@Override
public void onComplete(@NonNull Task<LocationSettingsResponse> task) {
try {
LocationSettingsResponse response = task.getResult(ApiException.class);
Toast.makeText(HomeActivity.this, "GPS is already tured on" + response, Toast.LENGTH_SHORT).show();
getCurrentLocation();
} catch (ApiException e) {
switch (e.getStatusCode()) {
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
try {
ResolvableApiException resolvableApiException = (ResolvableApiException) e;
resolvableApiException.startResolutionForResult(HomeActivity.this, REQUEST_CHECK_SETTINGS);
} catch (IntentSender.SendIntentException ex) {
ex.printStackTrace();
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
//Device does not have location
break;
}
}
}
});
}
});
GetLocation methode
@RequiresApi(api = Build.VERSION_CODES.M)
private void getCurrentLocation() {
int picd = 0;
if (picd == 0) {
if (!checkLocationFinePermission() || !checkLocationCoarsePermission() ) {
requestLocationPermission();
} else {
fusedLocationProviderClient.getLastLocation().addOnCompleteListener(new OnCompleteListener<Location>() {
@Override
public void onComplete(@NonNull Task<Location> task) {
try {
//initialize geoCoder
Location location = task.getResult();
Geocoder geocoder = new Geocoder(HomeActivity.this
, Locale.getDefault());
//initialize adresse
List<Address> addresses = geocoder.getFromLocation(
location.getLatitude(), location.getLongitude(), 1
);
//setAdresse
adresseEditText.setText(addresses.get(0).getAddressLine(0));
} catch (IOException e) {
e.printStackTrace();
Log.i("TAG", "onComplete: " + e);
}
}
});
}
}