5

I'm having the following error on android 21 and above.

I'm having the following error, and there is no restriction on manufacturer or model for the error.

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String[] android.content.pm.PackageManager.getPackagesForUid(int)' on a null object reference
    at android.os.Parcel.readException(Parcel.java:1605)
    at android.os.Parcel.readException(Parcel.java:1552)
    at android.location.ILocationManager$Stub$Proxy.getLastLocation(ILocationManager.java:717)
    at android.location.LocationManager.getLastKnownLocation(LocationManager.java:1200)
    at [...]

The call that triggers the error is either one bellow:

mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

I check beforehand if the mLocationManager is not null or if I have the necessary permissions. It work on most cases but I do have some crashing occurrences.

Does anyone knows what can be done to avoid it, or if there is any way to detect that it will happen so I don't request the location?

Thanks for your attention,

Gabriel Falcone
  • 678
  • 5
  • 17
  • Just to understand, when you say some occurrences, is it on same device you see it crashing sometimes and working other times? – prasunnair Dec 16 '15 at 13:37
  • Seems to be something in lines of the following http://stackoverflow.com/questions/23906926/locationmanager-getlastknownlocation-return-null – prasunnair Dec 16 '15 at 13:39
  • 1
    The crash monitor tool is detecting it on the store app. I haven't received it while developing. It not that often, but I have received over 100 occurrences. – Gabriel Falcone Dec 16 '15 at 13:40
  • Please check the link I posted. It seems getLastKnownLocation is not foolproof. A coldStart of GPS might return null for the device. As mentioned on another answer and also on the link provided for reliable location it is better to use LocationListener – prasunnair Dec 16 '15 at 13:42
  • 1
    He is getting null as the method's response. My code is crashing before the response is returned. I think they are different issues. Prasunnair, I think it is a different issue. My app is handling well when there isn't location available. – Gabriel Falcone Dec 16 '15 at 13:43
  • Gabriel, were you able to solve the issue? I have the same issue. Thanks! – Abushawish Jan 27 '16 at 00:01
  • @StackPWRequirmentsAreCrazy, I still do not have a solution for this problem. I am catching the exception and blocking location services until the end of that session. – Gabriel Falcone Jan 27 '16 at 00:05
  • Does this usually happen to you when the phone just starts/location is very recently turned on too? – Abushawish Jan 27 '16 at 00:09
  • I haven't got this issue on my own devices up to this point. This error I receive in production only. Can you actually reproduce it? – Gabriel Falcone Jan 27 '16 at 00:29

0 Answers0