位置管理器返回NULL指针厚望 [英] Location Manager Returns Null Pointer Expection
问题描述
请我有这个code已经为我工作而所有这一切,突然它是不工作更多的,则返回空指针异常的 双纬度=(双)(lastKnownLocation.getLatitude()); 我已经设定在清单文件中的所有所需的权限,但它似乎没有再工作。我想一定是现在使用全球定位系统提供的一个问题。
latituteField =(TextView中)findViewById(R.id.TextViewLatitudeValue);
longitudeField =(TextView中)findViewById(R.id.TextViewLongitudeValue);
SpeedField =(TextView中)findViewById(R.id.textViewSpeedValue);
AltitudeField =(TextView中)findViewById(R.id.textViewAltitudeValue);
AddressLabel =(TextView中)findViewById(R.id.textViewAddress);
lbllatitude =(TextView中)findViewById(R.id.lblLatDMS);
lbllongitude =(TextView中)findViewById(R.id.lblLongDMS);
//获取位置管理器
locationManager =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
// GPS_PROVIDER
字符串locationProvider = LocationManager.GPS_PROVIDER;
位置lastKnownLocation = locationManager.getLastKnownLocation(locationProvider);
LocationManager服务=(LocationManager)getSystemService(LOCATION_SERVICE);
布尔启用= service.isProviderEnabled(LocationManager.GPS_PROVIDER);
//检查是否允许,如果不发送用户对GSP的设置
//更好的解决办法是显示一个对话框,并建议到
//进入设置
如果(!启用)
{
意向意图=新的意图(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(意向);
}
否则,如果(locationProvider!= NULL)
{
Toast.makeText(此,locationProvider +被选择,Toast.LENGTH_SHORT).show();
onLocationChanged(lastKnownLocation);
}
其他
{
latituteField.setText(0.00);
longitudeField.setText(0.00);
AltitudeField.setText(0.00);
SpeedField.setText(0.00);
}
}
/ *请求更新在启动* /
@覆盖
保护无效onResume(){
super.onResume();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,400,1,本);
}
/ *删除LocationListener的更新时,活动暂停* /
@覆盖
保护无效的onPause(){
super.onPause();
locationManager.removeUpdates(本);
}
@覆盖
公共无效onLocationChanged(位置lastKnownLocation){
双纬度=(双)(lastKnownLocation.getLatitude());
双LNG =(双)(lastKnownLocation.getLongitude());
双Alt键=(双)(lastKnownLocation.getAltitude());
双速=(双)(lastKnownLocation.getSpeed());
latituteField.setText(的String.Format(%5F,LAT));
longitudeField.setText(的String.Format(%5F,LNG));
AltitudeField.setText(的String.Format(%5F,Alt键));
SpeedField.setText(的String.Format(%5F,速度));
LastKnownLocation将返回null如果任它的位置太旧,或者如果供应商从未被开启。你必须考虑到这一点。就一定要得到一个有效的位置,唯一的办法就是请求更新,并把它在onLocationChanged。
此外,可能lastKnownLocation返回一个值,该值是非常错 - 的东西从10或15分钟前是可能的。您可以使用它,但不要期望准确性。
Please I have this code which has been working for me all this while , all of a sudden it is not working any more , it returns null pointer exception at double lat = (double) (lastKnownLocation.getLatitude());. I have set all required permission at the manifest file , but it seems not to work again. I think there must be a problem using the GPS provider now.
latituteField = (TextView) findViewById(R.id.TextViewLatitudeValue);
longitudeField = (TextView) findViewById(R.id.TextViewLongitudeValue);
SpeedField = (TextView) findViewById(R.id.textViewSpeedValue);
AltitudeField = (TextView) findViewById(R.id.textViewAltitudeValue);
AddressLabel=(TextView)findViewById(R.id.textViewAddress);
lbllatitude=(TextView)findViewById(R.id.lblLatDMS);
lbllongitude=(TextView)findViewById(R.id.lblLongDMS);
// Get the location manager
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// GPS_PROVIDER
String locationProvider = LocationManager.GPS_PROVIDER;
Location lastKnownLocation = locationManager.getLastKnownLocation(locationProvider);
LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);
boolean enabled = service.isProviderEnabled(LocationManager.GPS_PROVIDER);
// check if enabled and if not send user to the GSP settings
// Better solution would be to display a dialog and suggesting to
// go to the settings
if (!enabled)
{
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
else if (locationProvider!= null)
{
Toast.makeText(this, locationProvider +" has been selected",Toast.LENGTH_SHORT).show();
onLocationChanged(lastKnownLocation);
}
else
{
latituteField.setText("0.00");
longitudeField.setText("0.00");
AltitudeField.setText("0.00");
SpeedField.setText("0.00");
}
}
/* Request updates at startup */
@Override
protected void onResume() {
super.onResume();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 400, 1, this);
}
/* Remove the locationlistener updates when Activity is paused */
@Override
protected void onPause() {
super.onPause();
locationManager.removeUpdates(this);
}
@Override
public void onLocationChanged(Location lastKnownLocation) {
double lat = (double) (lastKnownLocation.getLatitude());
double lng = (double) (lastKnownLocation.getLongitude());
double Alt = (double) (lastKnownLocation.getAltitude());
double Speed = (double) (lastKnownLocation.getSpeed());
latituteField.setText(String.format("%.5f",lat));
longitudeField.setText(String.format("%.5f",lng));
AltitudeField.setText(String.format("%.5f",Alt));
SpeedField.setText(String.format("%.5f",Speed));
LastKnownLocation will return null if either the location it has is too old or if the provider has never been turned on. You must take this into account. THe only way to be sure to get a valid location is to request updates and get it in onLocationChanged.
ALso, lastKnownLocation may return a value that's very wrong- something from 10 or 15 minutes ago is possible. You can use it, but don't expect accuracy.
这篇关于位置管理器返回NULL指针厚望的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!