Android : Get current location in a static context

on Tuesday, April 14, 2015


I'm just starting studying android. I have a Navigation Drawer Activity. I'm trying to get my current location. This is my code:



public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View rootView=inflater.inflate(R.layout.fragment_main, container, false);
int sectionNumber = getArguments().getInt(ARG_SECTION_NUMBER);
switch (sectionNumber) {
case 1:
rootView = inflater.inflate(R.layout.fragment_monitor, container, false);
TextView lat = (TextView) rootView.findViewById(R.id.lat);
LocationManager locationmanager;
String context=Context.LOCATION_SERVICE;
locationmanager=(LocationManager) getSystemService(context);
String provider=LocationManager.NETWORK_PROVIDER;
Location location= locationmanager.getLastKnownLocation(provider);

double lat = location.getLatitude();
double lon = location.getLongitude();

break;
case 2:
rootView = inflater.inflate(R.layout.activity_mapa, container, false);
break;
case 3:
break;
default:
rootView = inflater.inflate(R.layout.fragment_main, container, false);
}
return rootView;
}


It has an error in getSystemService() method. It says that a non-static method cannot be referenced from a static context.


Anyone knows how to solve this?


thanks.


0 comments:

Post a Comment