【发布时间】:2013-05-01 16:48:01
【问题描述】:
我创建了一个应用程序,用于显示具有网络提供商或 GPS 提供商用户选择的位置 通过复选框。当用户选择不启用 GPS 时,工作正常。但是当用户在关闭区域调试后第一次选择 GPS 启用时进入家庭崩溃......在开放区域工作重复后......并且没有问题的工作和家庭内部。 会发生什么?我如何避免此错误?需要使用不同的代码应用程序更改我的代码自动选择最佳提供商?(不是用户)这是我的代码....
// Get the location manager
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// Define the criteria how to select the locatioin provider -> use
// default
if(gps_en==true){ //In previous activity with checkbox user choice preferences
provider = LocationManager.GPS_PROVIDER;
Location location = locationManager.getLastKnownLocation(provider);
// Initialize the location fields
if (location != null) {
System.out.println("Provider " + provider + " has been selected.");
onLocationChanged(location);
} else {
latituteField.setText("Location not available");
longitudeField.setText("Location not available");
}
}
else{
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(provider);
// Initialize the location fields
if (location != null) {
System.out.println("Provider " + provider + " has been selected.");
onLocationChanged(location);
} else {
latituteField.setText("Location not available");
longitudeField.setText("Location not available");
}
}
java.lang.NullPointerException
loc.LocTracker.onSensorChanged(LocTracker.java:769)
android.hardware.SensorManager$ListenerDelegate$1.handleMessage(SensorManager.java:580)
android.os.Handler.dispatchMessage(Handler.java:99)
android.os.Looper.loop(Looper.java:137)
android.app.ActivityThread.main(ActivityThread.java:4482)
java.lang.reflect.Method.invokeNative(Native Method)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:561)
dalvik.system.NativeStart.main(Native Method)
您能否为我的代码中的这一点建议一种更好的方法? 权限没问题。我想要应用程序自动选择最佳提供商 GPS 或网络 首先是准确性标准。 从网络提供商开始,如果 GPS 提供商具有更好的准确性,如果 用户在使用 GPS 提供商的开放区域应用程序中。 谢谢。
【问题讨论】:
-
您应该发布显示崩溃/异常的 LogCat sn-p。
-
我用 LogCat 更新它
标签: android networking gps location provider