nwLocation
& gpsLocation
are null at onStart()
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
FrgAddNewJobsLocation.java (class extends Fragment)
@Override
public void onStart() {
super.onStart();
initilizeMap();
appLocationService = new AppLocationService(getActivity());
nwLocation = appLocationService.getLocation(LocationManager.NETWORK_PROVIDER);
gpsLocation = appLocationService.getLocation(LocationManager.GPS_PROVIDER);
}
AppLocationService.java
public class AppLocationService extends Service implements LocationListener {
protected LocationManager locationManager;
Location location;
private static final long MIN_DISTANCE_FOR_UPDATE = 10;
private static final long MIN_TIME_FOR_UPDATE = 1000 * 60 * 2;
public AppLocationService(Context context) {
locationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);
}
public Location getLocation(String provider) {
if (locationManager.isProviderEnabled(provider)) {
locationManager.requestLocationUpdates(provider, MIN_TIME_FOR_UPDATE, MIN_DISTANCE_FOR_UPDATE, this);
if (locationManager != null) {
location = locationManager.getLastKnownLocation(provider);
return location;
}
}
return null;
}
@Override
public void onLocationChanged(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;
}
}
Instead of appLocationService = new AppLocationService(getActivity());
use:
Context mcontext;
appLocationService = new AppLocationService(mContext);
Also check if you have provided internet permission in manifest file
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.