title: Android: can't get current location on maps
body: <p>I have an app with google maps with one single activity where a map is displayed. I have a menu that allows me to change the map type from but I would like to have an option to get my current location.</p>
  
  <p>Here's the code of my activity:</p>
  
  <pre><code>package com.example.chiapa_mapas;
  
  import com.google.android.gms.location.LocationListener;
  import com.google.android.gms.maps.CameraUpdateFactory;
  import com.google.android.gms.maps.GoogleMap;
  import com.google.android.gms.maps.GoogleMap.OnMapClickListener;
  import com.google.android.gms.maps.SupportMapFragment;
  import com.google.android.gms.maps.model.BitmapDescriptorFactory;
  import com.google.android.gms.maps.model.LatLng;
  import com.google.android.gms.maps.model.MarkerOptions;
  
  import android.content.Context;
  import android.location.Criteria;
  import android.location.Location;
  import android.location.LocationManager;
  import android.os.Bundle;
  import android.view.MenuInflater;
  import android.view.MenuItem;
  
  public class MainActivity extends android.support.v4.app.FragmentActivity implements OnMapClickListener, LocationListener{
  
      private GoogleMap mMap;
      private Context ctx;
      private LocationManager locationManager;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          // Try to obtain the map from the SupportMapFragment.
          mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                  .getMap();
          mMap.setOnMapClickListener(this);
  
  
       // Enable LocationLayer of Google Map
          mMap.setMyLocationEnabled(true);
          // Getting LocationManager object from System Service LOCATION_SERVICE
          locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
  
  
          }
  
      @Override
      public boolean onCreateOptionsMenu(android.view.Menu menu) {
          MenuInflater inflater = getMenuInflater();
          inflater.inflate(R.layout.opcoes, menu);
          return true; }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          switch (item.getItemId()) {
  
          case R.id.normal:
              mMap.setMapType(1);
              break;
  
          case R.id.satellite:
              mMap.setMapType(2);
              break;
  
          case R.id.terrain:
              mMap.setMapType(3);
              break;
          case R.id.hybrid:
               mMap.setMapType(4);
               break;
  
          case R.id.none:
              mMap.setMapType(0);
              break;
  
          case R.id.posicao_actual:
              //mMap.setMapType(0);
              Criteria criteria = new Criteria();
              // Getting the name of the best provider
              String provider = locationManager.getBestProvider(criteria,true);
              // Getting Current Location
              Location location = locationManager.getLastKnownLocation(provider);
              if (location != null) {
                  onLocationChanged(location); 
                  } 
  
              locationManager.requestLocationUpdates(provider, 20000, 0,(android.location.LocationListener) ctx);
  
              break;
  
          default: return super.onOptionsItemSelected(item);
          }
  
          return true;
      }
  
      @Override
      public void onMapClick(LatLng position) {
          // TODO Auto-generated method stub
  
          mMap.addMarker(new MarkerOptions().position(position).icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher)));
  
          //double ltt = position.latitude;
          //double lgt = position.longitude;
  
          //String msg = "Latitude: " + Double.toString(ltt) + " , Longitude: " + Double.toString(lgt) + "";
  
          //Toast toast = Toast.makeText(ctx, msg, Toast.LENGTH_SHORT);
          //toast.show();     
      }
  
      @Override
      public void onLocationChanged(Location location) {
          // TODO Auto-generated method stub
  
          // Getting latitude of the current location
          double latitude = location.getLatitude();
          // Getting longitude of the current location
          double longitude = location.getLongitude();
          // Creating a LatLng object for the current location
          LatLng latLng = new LatLng(latitude, longitude);
          // Showing the current location in Google Map
          mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
          // Zoom in the Google Map
          mMap.animateCamera(CameraUpdateFactory.zoomTo(15));
          // Setting latitude and longitude in the TextView
  
          locationManager.removeUpdates((android.location.LocationListener) this);
  
      }
  
  }
  </code></pre>
  
  <p>Can someone help? When I press the option "posicao actual" (current location) it crashes.
  Thanks in advance
  Chiapa</p>