I run into the NullPointerException
while calling getSystemService()
. I checked some posts before but can't figure out why it failed.
public class GetGyroInfo extends Service implements LocationListener{
private float mLongAccl;
private float mLatAccl;
private static final long MIN_TIME_BW_UPDATES = 100; // 100 millisecond
//Declaring a location manager
private LocationManager mLocationManager = null;
private Location mLocation = null;
private static final String TAG = "ADAS_GetGyroInfo";
public void getLocation(Context context){
//Note: getApplicationContext returns NULL inside this constructor. Reason not clear.
//Create a registor for location updates
mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
...
}
The above class is initialized in the following class:
public class ABCService extends Service {
...
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
sendStatusBroadcast(true);
//Registor location service.
mgetGyroInfo = new GetGyroInfo();
mgetGyroInfo.getLocation();
}
I tried the following code and it succeeds:
mgetGyroInfo.getLocation(getApplicationContext());
So my question is, why I have to pass the context from ABCService
class to GetGyroInfo
? Why can't I get the context inside GetGyroInfo
itself?