Many people often encounter this problem, mainly to obtain the location of the information is null, the first major to have permission
<uses-permission android:name= "Android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name= "Android.permission.ACCESS_MOCK_LOCATION"/>
The second method we call is location location = locationmanager.getlastknownlocation (provider);
This method is mainly to obtain the last latitude and longitude information, the last word must be empty. This is to register the listener that gets the event
After a while you can get the latitude and longitude. So don't worry about getting latitude and longitude at first. Wait a minute. Null pointer exceptions are not reported.
Locationmanager Locationmanager;
String serviceName = Context.location_service;
Locationmanager = (locationmanager) con.getsystemservice (serviceName);//Find service Information
//locationmanager.settestproviderenabled ("GPs", true);
locationmanager.requestlocationupdates (locationmanager.gps_provider, 0, MLocationListener01);
locationmanager.requestlocationupdates (locationmanager.network_provider, 0, MLocationListener01);
Get Data and listeners
* Get the exact location of the current device
*
* @param location
* @param rowId
* @param params
* @param rowId
*/
Private location Updatetonewlocation (location location) {
System.out.println ("--------zhixing--2--------");
String latlongstring;
Double lat = 0;
Double lng=0;
if (location! = null) {
lat = Location.getlatitude ();
LNG = Location.getlongitude ();
latlongstring = "Latitude:" + lat + "\ n Longitude:" + LNG;
System.out.println ("Longitude:" +lng+ "Latitude:" +lat);
} else {
latlongstring = "Unable to obtain geographic information, please later ...";
}
if (lat!=0) {
System.out.println ("--------feedback----------" + string.valueof (LAT));
}
Toast.maketext (Getapplicationcontext (), latlongstring, Toast.length_short). Show ();
return location;
}
//set listener, the minimum time for automatic Updates is interval n seconds (1 seconds for 1*1000, so write mainly for convenience) or minimum displacement change more than N m
Public final Locationlistener MLocationListener01 = new Locationlistener () {
& nbsp @Override
public void onlocationchanged (location location) {
updatetonewlocation (location);
&NBSP;}
@Override
Public void onproviderdisabled (String provider) {
updatetonewlocation (NULL);
}
@Override
Public void onproviderenabled (String provider) {}
@Override
Public void Onstatuschanged (String provider, int status, Bundle extras) {}
};
Tested: No problem.. Hope to help you ...
Android Gets the location null problem