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
  • 16
  • 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

1 Answers1

0

Use this class to get location in android

GpsTracker.java

public class GpsTracker extends Service implements LocationListener {
private final Context mContext;

// flag for GPS status
boolean isGPSEnabled = false;

// flag for network status
boolean isNetworkEnabled = false;

// flag for GPS status
boolean canGetLocation = false;

Location location; // location
double latitude; // latitude
double longitude; // longitude

// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters

// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000; // 1 second

// Declaring a Location Manager
protected LocationManager locationManager;

public GpsTracker(Context context) {
    this.mContext = context;
    getLocation();
}

public Location getLocation() {

    if (Build.VERSION.SDK_INT >= 23 &&
            ContextCompat.checkSelfPermission(mContext, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
            ContextCompat.checkSelfPermission(mContext, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return null;
    }

    try {
        locationManager = (LocationManager) mContext
                .getSystemService(LOCATION_SERVICE);

        // getting GPS status
        isGPSEnabled = locationManager
                .isProviderEnabled(LocationManager.GPS_PROVIDER);

        // getting network status
        isNetworkEnabled = locationManager
                .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        if (!isGPSEnabled && !isNetworkEnabled) {
            // no network provider is enabled
        } else {
            this.canGetLocation = true;
            // First get location from Network Provider
            if (isNetworkEnabled) {
                locationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER,
                        MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                Log.d("Network", "Network");
                if (locationManager != null) {
                    location = locationManager
                            .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    if (location != null) {
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                    }
                }
            }
            // if GPS Enabled get lat/long using GPS Services
            if (isGPSEnabled) {
                if (location == null) {
                    locationManager.requestLocationUpdates(
                            LocationManager.GPS_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("GPS Enabled", "GPS Enabled");
                    if (locationManager != null) {
                        location = locationManager
                                .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return location;
}

/**
 * Stop using GPS listener
 * Calling this function will stop using GPS in your app
 * */
public void stopUsingGPS() {
    if (locationManager != null) {
        if (Build.VERSION.SDK_INT >= 23 &&
                ContextCompat.checkSelfPermission(mContext, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
                ContextCompat.checkSelfPermission(mContext, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return ;
        }
        locationManager.removeUpdates(GpsTracker.this);
    }       
}



/**
 * Function to get latitude
 * */
public double getLatitude(){
    if(location != null){
        latitude = location.getLatitude();
    }

    // return latitude
    return latitude;
}

/**
 * Function to get longitude
 * */
public double getLongitude(){
    if(location != null){
        longitude = location.getLongitude();
    }

    // return longitude
    return longitude;
}

/**
 * Function to check GPS/wifi enabled
 * @return boolean
 * */
public boolean canGetLocation() {
    return this.canGetLocation;
}

/**
 * Function to show settings alert dialog
 * On pressing Settings button will lauch Settings Options
 * */
public void showSettingsAlert(){
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);

    // Setting Dialog Title
    alertDialog.setTitle("GPS settings");

    // Setting Dialog Message
    alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");

    // On pressing Settings button
    alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog,int which) {
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            mContext.startActivity(intent);
            ((Activity)mContext).finish();
        }
    });

    // on pressing cancel button
    alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
        dialog.cancel();
        }
    });

    // Showing Alert Message
    alertDialog.show();
}

@Override
public void onLocationChanged(Location location) 
{
    this.location = location;
}

@Override
public void onProviderDisabled(String provider) {
}

@Override
public void onProviderEnabled(String provider) {
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}

@Override
public IBinder onBind(Intent arg0) {
    return null;
}
}
pRaNaY
  • 23,128
  • 23
  • 90
  • 142
  • 1
    Thanks for your response, but my code already do all those checks. I even do a little more while waiting for the location to be received. The problem is after doing all those checks, when I do the following call: 'location = locationManager .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);' I still have the error, not often but I do receive, regardless of all those checks. Should the answer be only to do a try-catch exception and Ignore the error? I really wanted to avoid it. – Gabriel Falcone Dec 16 '15 at 13:49
  • 1
    Your class, works without problem until the user turn off the cellphone and then turn on the cellphone. I used that class in my app and I had that issue. @Er. Rakesh Prajapat – EagleCode16 Dec 16 '15 at 14:02
  • @Coeus please check respective permission for that. – Er. Rakesh Prajapat Dec 16 '15 at 14:04
  • Done! I found that class in internet but as I said, I got this issue. – EagleCode16 Dec 16 '15 at 14:06