title: locationManager.getLastKnownLocation() returns null
body: <p>I am making a program and i want to get gps coordinates  of my device.The problem is that when locationManager.getLastKnownLocation() is called it returns null.My device runs android 4.4 API 19.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>import android.Manifest;
  import android.app.Activity;
  import android.content.Context;
  import android.location.Location;
  import android.location.LocationListener;
  import android.location.LocationManager;
  import android.os.Bundle;
  import android.support.v4.app.ActivityCompat;
  import android.util.Log;
  import android.widget.TextView;
  import android.widget.Toast;
  
  public class MainActivity extends Activity implements LocationListener {    
  
  private TextView latituteField;
  
  private TextView longitudeField;
  private LocationManager locationManager;
  private String provider;
  
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      latituteField = (TextView) findViewById(R.id.TextView02);
      longitudeField = (TextView) findViewById(R.id.TextView04);
  
      locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
  
      provider = LocationManager.GPS_PROVIDER;
      if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &amp;&amp; ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
          // TODO: Consider calling
          //    ActivityCompat#requestPermissions
          // here to request the missing permissions, and then overriding
          //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
          //                                          int[] grantResults)
          // to handle the case where the user grants the permission. See the documentation
          // for ActivityCompat#requestPermissions for more details.
          return;
      }
      Location location = locationManager.getLastKnownLocation(provider);
  
      // Initialize the location fields
      if (location != null) {
          System.out.println("Provider " + provider + " has been selected.");
          onLocationChanged(location);
      } else {
          latituteField.setText("Location not available");
          longitudeField.setText("Location not available");
      }
  }
  
  /* Request updates at startup */
  @Override
  protected void onResume() {
      super.onResume();
      if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &amp;&amp; ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
          // TODO: Consider calling
          //    ActivityCompat#requestPermissions
          // here to request the missing permissions, and then overriding
          //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
          //                                          int[] grantResults)
          // to handle the case where the user grants the permission. See the documentation
          // for ActivityCompat#requestPermissions for more details.
          return;
      }
      locationManager.requestLocationUpdates(provider, 400, 0, this);
  }
  
  /* Remove the locationlistener updates when Activity is paused */
  @Override
  protected void onPause() {
      super.onPause();
      locationManager.removeUpdates(this);
  }
  
  @Override
  public void onLocationChanged(Location location) {
      Log.d("location change:", " change1  " );
      int lat = (int) (location.getLatitude());
      int lng = (int) (location.getLongitude());
      latituteField.setText(String.valueOf(lat));
      longitudeField.setText(String.valueOf(lng));
  }
  
  @Override
  public void onStatusChanged(String provider, int status, Bundle extras) {
      // TODO Auto-generated method stub
  
  }
  
  @Override
  public void onProviderEnabled(String provider) {
      Toast.makeText(this, "Enabled new provider " + provider,
              Toast.LENGTH_SHORT).show();
  
  }
  
  @Override
  public void onProviderDisabled(String provider) {
      Toast.makeText(this, "Disabled provider " + provider,
              Toast.LENGTH_SHORT).show();
  }
  </code></pre>
  
  <p>}</p>
  
  <p>and this is my Manifest file:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.example.vaggelis.location"&gt;
  
  &lt;uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /&gt;
  &lt;uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /&gt;
  &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
  
  &lt;application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:roundIcon="@mipmap/ic_launcher_round"
      android:supportsRtl="true"
      android:theme="@style/AppTheme"&gt;
      &lt;activity android:name=".MainActivity"&gt;
          &lt;intent-filter&gt;
              &lt;action android:name="android.intent.action.MAIN" /&gt;
  
              &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
          &lt;/intent-filter&gt;
      &lt;/activity&gt;
  &lt;/application&gt;
  </code></pre>
  
  <p></p>
  
  <p>What am i doing wrong?</p>