I'm simply trying to write a method that returns Latitude and Longitude. I have an activity with a button and two text fields. The java class I am using extends AppCompatActivity
and implements LocationListener
When the button is pressed the following method is pressed:
public void startGPS(View view)
{
// Here is the code to handle permissions - you should not need to edit this.
if ( Build.VERSION.SDK_INT >= 23 &&
ContextCompat.checkSelfPermission( this, android.Manifest.permission.ACCESS_FINE_LOCATION ) != PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission( this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[] { android.Manifest.permission.ACCESS_FINE_LOCATION, android.Manifest.permission.ACCESS_COARSE_LOCATION }, TAKE_PHOTO_PERMISSION);
}
Location location = locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 5000, 0, this);
}
Later on I try to print out my location in my onLocationChanged
method but it was causing the app to crash. I ran it through the debugger and found that the location was always null
.
I tried to this solution but it didn't work. Other examples are calling the function in onResume
but I really need this to be in startGPS
method.
Also, is there a chance that the error is just with my device? I'm running it on a Nexus 7 which doesn't seem to have any problems when I run Google Maps.