0

I have a little app to get the Location and then show it on the map.

In onCreate() I start the LocationService and the map. I then want to call my setLocation() method to determine my location and show it on the map.

If i call the setLocation() method from onCreate() it does not work and the toast gets shown. If i call setLocation() from onResume() it works. What could be the reason for that?

Source here: Github

Thanks for any help. If you need a certain part of the rest of the code just tell me.

  • We do not know what `mapActivity` is, we do not know what `GPSService` is, we do not know what `getPos()` on `GPSService` does, etc. – CommonsWare Jan 09 '16 at 14:42
  • added the whole source – strange-matter Jan 09 '16 at 14:52
  • have you tried using the debugger in the service call with breakpoints to see why the returned location is null? – Mo1989 Jan 09 '16 at 15:04
  • Also the way you seem to have it the GPS service is pretty useless since you access it directly from the activity using to a synchronous method call – Mo1989 Jan 09 '16 at 15:08
  • It seems like the OnMapReady() gets called after setLocation, thus mapActivity is null, altough I create the mapFragment first, what could be the reason for that? – strange-matter Jan 09 '16 at 15:18
  • @Override public void onMapReady (GoogleMap googleMap) { mapActivity = new MapsActivity(googleMap); } . In onCreate(), mapActivity is still null. – tiny sunlight Jan 09 '16 at 16:11
  • Take a look here for a simple example of a complete implementation of this functionality: http://stackoverflow.com/a/34582595/4409409 – Daniel Nugent Jan 09 '16 at 17:19

1 Answers1

0

Never create service instances yourself via the constructor. There is no reason at all for GPSService to exist, let alone be a Service. That being said, getPos() will return null until such time as you get a location fix. Exactly when that will occur is indeterminate.

Similarly, never create activity instances yourself via a constructor. There is no reason at all for MapsActivity to exist, let alone to be an actual subclass of Activity. That being said, the mapActivity field in MainActivity will be null until onMapAsync() is called. Exactly when that will occur is also indeterminate.

Hence, you cannot reliably call setLocation() from either onCreate() or onResume(). After all, you may not be able to actually get a location ever.

Moreover, your application will crash on Android 6.0+ devices, if your targetSdkVersion is 23 or higher, as you are attempting to get locations before you even ask for the runtime permission, let alone have it be granted.

Your mapActivity.moveCamera(location) call cannot be made until you have a location and you have a map. You will need to arrange to see, in onMapReady() and onLocationChanged(), whether you have both pieces of data, and then call mapActivity.moveCamera(location).

Also, FWIW, your WRITE_EXTERNAL_STORAGE <uses-permission> element is in the wrong place in the manifest. <uses-permission> elements have to be the immediate children of the root <manifest> element, not children of <application>.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thanks first for the answer, I'm new to Android development, I'm haopy about such advice. So you suggest putting that all in the MainActivity and not splitting it up into several classes? How can i get sure everthing gets called in correct order? – strange-matter Jan 09 '16 at 15:31
  • @strange-matter: "So you suggest putting that all in the MainActivity and not splitting it up into several classes?" -- for the level of functionality that you presently have, I do not see where the extra classes are giving you any benefit. "How can i get sure everthing gets called in correct order?" -- well, to a large extent, that comes with experience. Technically, have `onMapReady()` and `onLocationChanged()` call some common method that sees if you have met both requirements (map + location), where that method then calls `moveCamera()` once both requirements are met. – CommonsWare Jan 09 '16 at 15:48