Alex
Alex

Reputation: 3

Getting Users Geo Location API 23

Every time I run this, I get this error message "java.lang.IllegalStateException: System services not available to Activities before onCreate()"

I need to get the users longitude and latitude so I can use it for the rest of my app, but I cannot figure out how to get past this error.

    public class map extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;
private FloatingActionButton plus;
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double longitude = location.getLongitude();
double latitude = location.getLatitude();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_map);

    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

    plus = (FloatingActionButton) findViewById(R.id.newPlace);

    plus.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            startActivity(new Intent(map.this, newPlacePop.class));
        }
    });
}



@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    LatLng currentPosition = new LatLng(latitude, longitude);

    float zoomLevel = 16; //This goes up to 21
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(currentPosition, zoomLevel));

}

}

Upvotes: 0

Views: 87

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006819

java.lang.IllegalStateException: System services not available to Activities before onCreate()

You have a field initializer that is calling getSystemService():

LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

You cannot call getSystemService() — or most other methods that you are inheriting from Activity — before the call to super.onCreate() in your onCreate() method.

So, change:

LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double longitude = location.getLongitude();
double latitude = location.getLatitude();

to:

LocationManager lm;
Location location;
double longitude;
double latitude;

and add the following lines after super.onCreate() in your onCreate() method:

lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);

if (location==null) {
  // do something
}
else {
  longitude = location.getLongitude();
  latitude = location.getLatitude();
}

I need to get the users longitude and latitude so I can use it for the rest of my app

Your code is unlikely to get the latitude and longitude. getLastKnownLocation() frequently returns null, and you would crash with a NullPointerException without the if check in the code shown above. You may wish to read the documentation on getting location data.

Also, you mention "API 23". If your targetSdkVersion is 23 or higher, you need the code to request your location permission at runtime.

Upvotes: 1

Related Questions