title: How to get location using GPS in android?
body: <p>I want to detect my location, I am using <strong><code>ACCESS_FINE_LOCATION</code></strong> permission but when I run app, it crashes and on the screen occurs error message: </p>
  
  <blockquote>
    <p>"AppName" has stopped working</p>
  </blockquote>
  
  <p>Following is my code, I am using to implement the above said:</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
      LocationManager locationManager;
      Location location;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
  
         locationManager = (LocationManager) 
                         getSystemService(Context.LOCATION_SERVICE);
         if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
             return;
         }
         location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
         Log.v("My Coordinates are: ", location.getLatitude() + " " + location.getLongitude());
      }
  }
  </code></pre>
  
  <p>What am I doing wrong here? </p>
  
  <p>Please Help.</p>