我想使用的LocationManager获得的纬度,经度,等等。现在,当我运行code用的LocationManager下面的错误发生。
I am trying to use LocationManager to get the latitude,longitude, etc. Now when I am running the code with the LocationManager the below error is occurring.
02-17 09:31:10.473: E/AndroidRuntime(1238): FATAL EXCEPTION: main
02-17 09:31:10.473: E/AndroidRuntime(1238): java.lang.RuntimeException: Unable to start activity ComponentInfo{ com.example.testphonegap/com.example.testphonegap.MainActivity}: java.lang.IllegalArgumentException: invalid provider: null
02-17 09:31:10.473: E/AndroidRuntime(1238): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
02-17 09:31:10.473: E/AndroidRuntime(1238): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
02-17 09:31:10.473: E/AndroidRuntime(1238): at android.app.ActivityThread.access$600(ActivityThread.java:141)
02-17 09:31:10.473: E/AndroidRuntime(1238): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
02-17 09:31:10.473: E/AndroidRuntime(1238): at android.os.Handler.dispatchMessage(Handler.java:99)
02-17 09:31:10.473: E/AndroidRuntime(1238): at android.os.Looper.loop(Looper.java:137)
02-17 09:31:10.473: E/AndroidRuntime(1238): at android.app.ActivityThread.main(ActivityThread.java:5041)
02-17 09:31:10.473: E/AndroidRuntime(1238): at java.lang.reflect.Method.invokeNative(Native Method)
02-17 09:31:10.473: E/AndroidRuntime(1238): at java.lang.reflect.Method.invoke(Method.java:511)
02-17 09:31:10.473: E/AndroidRuntime(1238): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-17 09:31:10.473: E/AndroidRuntime(1238): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-17 09:31:10.473: E/AndroidRuntime(1238): at dalvik.system.NativeStart.main(Native Method)
02-17 09:31:10.473: E/AndroidRuntime(1238): Caused by: java.lang.IllegalArgumentException: invalid provider: null
02-17 09:31:10.473: E/AndroidRuntime(1238): at android.location.LocationManager.checkProvider(LocationManager.java:1594)
02-17 09:31:10.473: E/AndroidRuntime(1238): at android.location.LocationManager.requestLocationUpdates(LocationManager.java:425)
02-17 09:31:10.473: E/AndroidRuntime(1238): at com.example.testphonegap.MainActivity.onCreate(MainActivity.java:93)
02-17 09:31:10.473: E/AndroidRuntime(1238): at android.app.Activity.performCreate(Activity.java:5104)
02-17 09:31:10.473: E/AndroidRuntime(1238): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
02-17 09:31:10.473: E/AndroidRuntime(1238): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
我的code是如下:
My Code is as follows:
public class MainActivity extends DroidGap {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.init();
// new code
super.setBooleanProperty("keepRunning", true);
// ----------------------------------------------------------------------
super.setIntegerProperty("splashscreen", R.drawable.ldpi);
super.setStringProperty("loadingDialog", "Loading Silver Angel ...");
appView.addJavascriptInterface(this, "MainActivity");
super.setIntegerProperty("loadUrlTimeoutValue", 50000);
super.loadUrl("file:///android_asset/www/index.html",50000);
// New Code
LocationManager locationManager;
String context = Context.LOCATION_SERVICE;
locationManager = (LocationManager)getSystemService(context);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
String provider = locationManager.getBestProvider(criteria, true);
updateWithNewLocation(null);
locationManager.requestLocationUpdates(provider, (10*60*1000), 10, locationListener);
}
public void updateWithNewLocation(Location location) {
if (location != null) {
counter++;
latitude = location.getLatitude();
longitude = location.getLongitude();
//current speed fo the gps device
currentSpeed = round(location.getSpeed(),3,BigDecimal.ROUND_HALF_UP);
kmphSpeed = round((currentSpeed*3.6),3,BigDecimal.ROUND_HALF_UP);
//all speeds added together
totalSpeed = totalSpeed + currentSpeed;
totalKmph = totalKmph + kmphSpeed;
//calculates average speed
avgSpeed = round(totalSpeed/counter,3,BigDecimal.ROUND_HALF_UP);
avgKmph = round(totalKmph/counter,3,BigDecimal.ROUND_HALF_UP);
}
}
// new Code
private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
updateWithNewLocation(location);
}
public void onProviderDisabled(String provider){
updateWithNewLocation(null);
}
public void onProviderEnabled(String provider){ }
public void onStatusChanged(String provider, int status,
Bundle extras){ }
};
}
你能告诉我什么,我做错了吗?我需要用活套?如果是我在哪里需要把它?
Can you tell me what I am doing wrong please? Do I need to use Looper? If yes where do I need to put it?
感谢和放大器;问候,
基思Spiteri的
根据您的logcat的输出,你到位置管理器提供空提供商。
As per your logcat output, you are providing null provider to location manager.
使用低于code到getProvider和位置管理器设置。
Use below code to getProvider and set it with location manager.
locationManager = (LocationManager) context
.getSystemService(Service.LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
// ///Criteria //////////
crta = new Criteria();
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.GINGERBREAD) {
crta.setAccuracy(Criteria.ACCURACY_FINE);
}else{
crta.setAccuracy(Criteria.ACCURACY_MEDIUM);
}
crta.setPowerRequirement(Criteria.POWER_LOW);
String provider = locationManager.getBestProvider(crta, true);
locationManager.requestLocationUpdates(provider,
(10*60*1000), 10, YOUR_LOCATION_LISTENER);
将这个code到try catch块。
Put this code into try catch block.