0
I am developing a mobile application with Android Studio that aims to get the information of latitude and longitude and use the service of Google Geocoder to return the address of the geographical position. However, I am not able to capture the latitude and longitude of the cell phone. What is the problem of my code?
public class MainActivity extends AppCompatActivity {
private Location location;
private LocationManager locationManager;
private Address endereco;
double longitude = 0.0;
double latitude = 0.0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_denuncia );
if(ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){
// Check Permissions Now
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION,}, 1000);
return;
}else{
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
latitude = location.getLatitude();
longitude = location.getLongitude();
Log.i( "Longitude", String.valueOf( location.getLongitude() ) );
try {
endereco = buscarEndereco(latitude, longitude);
EditText editText = (EditText)findViewById(R.id.bairro);
editText.setText(endereco.getLocality(), TextView.BufferType.EDITABLE);
}catch (IOException e){
Log.i("GPS", e.getMessage());
} catch (Exception e) {
e.printStackTrace();
}
}
public Address buscarEndereco(double latitude, double longitude)throws Exception{
Geocoder geocoder;
Address address = null;
List<Address> addresses;
geocoder = new Geocoder(getApplicationContext());
addresses = geocoder.getFromLocation(latitude, longitude, 1);
if(addresses.size() > 0){
address = addresses.get(0);
}
return address;
}
Exit:
java.lang.Nullpointerexception: Attempt to invoke virtual method 'double android.location.Location.getLatitude()' on a null Object Reference
I’m suspicious with some kind of permission. Even with the GPS active it shows the GPS message disabled. Then I opened the apk google maps and returned to my application, then he managed to return to geolocation. However if I disable the gps, close the maps, activate the gps again and enter my application informs gps disabled. What could be?
– Carlos Diego
@Carlosdiego. I don’t know, but that would be something for another question. The problem of this one was the
NullPointerException
. If/when creating another question about this, I recommend putting the complete and compileable code in it, and clearly describing the steps needed to reproduce the problem.– Victor Stafusa
Okay, I’m going to analyze here, if by chance I can’t open a new question. But it worked partially.
– Carlos Diego
@Carlosdiego Having GPS enabled is not enough for that method
getLastKnownLocation()
return a value. To verify that the problem is the one that I indicate in my reply use the Google application and get your location, then run your.– ramaral