简体   繁体   English

位置跟踪在GPS中不起作用

[英]Location tracking is not working in GPS

I am facing a problem in the process of location tracking. 我在位置跟踪过程中遇到了一个问题。 When I track locations via the network provider it works but when I turn on the GPS it shows the error java.lang.NullPointerException at this line in my GPS tracker class: 当我通过网络提供商跟踪位置时,它可以工作,但是当我打开GPS时,它在我的GPS跟踪器类的这一行显示错误java.lang.NullPointerException

location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

Are there any conditions to solve? 有什么条件要解决吗?

GPS tracker.java: GPS tracker.java:

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);
            Log.i("location gpsssssssssssss", location.toString());
            if (location != null) {
                latitude = location.getLatitude();
                longitude = location.getLongitude();
            }
        }
    }
}

In the mylocationlistener class: 在mylocationlistener类中:

GPSTracker gps = new GPSTracker(mylocationlistener.this);
if (gps.canGetLocation()) {
    latiude = gps.getLatitude();
    String lat=String.valueOf(latiude);
    String longi=String.valueOf(longitude);
    Log.v("lat", lat);
    Log.v("longi", longi);
    Log.v("sededededed", "--=-=-=-=-=-=-=-=-=-=-=-");
    longitude = gps.getLongitude();
}

First of all you should check is there GPS is available in device or not because if device has A-GPS, GLONASS then locationManager.getProvider(LocationManager.GPS_PROVIDER) will return null and then you will have to use LocationManager.NETWORK_PROVIDER to get location from your Network/Wifi. 首先,您应该检查设备中是否有GPS,因为如果设备具有A-GPS,GLONASS,则locationManager.getProvider(LocationManager.GPS_PROVIDER)将返回null ,然后您必须使用LocationManager.NETWORK_PROVIDER从中获取位置您的网络/ Wifi。

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

    if (locationManager.getProvider(LocationManager.GPS_PROVIDER) == null) {
        Log.v(TAG, "GPS not available");
    }
    else
    {
        Log.v(TAG, "GPS available");
    }

You can use a class below: 您可以在下面使用一个类:

public class GpsProvider extends Service implements LocationListener {

private final Context mContext;

// flag for GPS status
boolean isGPSEnabled = 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 * 60 * 1; // 1 minute

// Declaring a Location Manager
protected LocationManager locationManager;

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

public Location getLocation() {
    try {
        locationManager = (LocationManager) mContext
                .getSystemService(LOCATION_SERVICE);
        // getting GPS status
        isGPSEnabled = locationManager
                .isProviderEnabled(LocationManager.GPS_PROVIDER);

        // if GPS Enabled get lat/long using GPS Services
        if (isGPSEnabled) {
            this.canGetLocation = true;
            if (location == null) {
                locationManager.requestLocationUpdates(
                        LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                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) {
        locationManager.removeUpdates(GpsProvider.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 is 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);
                }
            });

    // 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 arg0) {
    // TODO Auto-generated method stub

}

@Override
public void onProviderDisabled(String arg0) {
    // TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String arg0) {
    // TODO Auto-generated method stub

}

@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
    // TODO Auto-generated method stub

}

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}

}

看来您的locationManager对象为null,这就是为什么它引发NullPointerException

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM