定位方法调用崩溃在某些设备上 [英] Location Method Calls Crashes On Some Devices

查看:173
本文介绍了定位方法调用崩溃在某些设备上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用图形页面一个应用程序,并打印出用户的纬度,经度和水平精度的一些标签。这一切工作在我的HTC野火很好,但在我的SE XPERIA应用程序崩溃时,我尝试触摸 Location.getLatitude() Location.getLongitude () Location.getAccuracy()

我有一种预感,这可能是对的Xperia GPS的速度比较慢,位置管理器还没有当我的纬度,经度和准确性轮询获得其坐标 - 但如何防范呢?

下面的代码片段:

  mapView.setBuiltInZoomControls(假);
        MC = mapView.getController();
        INT maxLat =(INT)(34.07687 * 1E6);
        INT maxLon =(INT)(-118.438239 * 1E6);
        INT minLat =(INT)(34.06489 * 1E6);
        INT MINLON =(INT)(-118.452358 * 1E6);
        清单<&叠加GT;覆盖=调用MapView.getOverlays();        MyLocationOverlay myLocationOverlay =新MyLocationOverlay(这一点,MapView类);
        myLocationOverlay.enableMyLocation();
        overlays.add(myLocationOverlay);        mc.zoomToSpan(Math.abs(maxLat - minLat),Math.abs(maxLon - MINLON));        LM =(的LocationManager)getSystemService(Context.LOCATION_SERVICE);
        地点= lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);        GP的GeoPoint =新的GeoPoint((INT)(location.getLatitude()* 1E6),(INT)(location.getLongitude()* 1E6));

logcat的输出:

  13 01-09:32:04.086:W / dalvikvm(1794):主题ID = 1:螺纹未捕获的异常退出(组= 0x2aac8560)
01-09 13:32:04.086:E / AndroidRuntime(1794):致命异常:主要
01-09 13:32:04.086:E / AndroidRuntime(1794):显示java.lang.NullPointerException
01-09 13:32:04.086:E / AndroidRuntime(1794):在no.tibeapp.sno.UlovligeGarnActivity $ 1.run(UlovligeGarnActivity.java:180)
01-09 13:32:04.086:E / AndroidRuntime(1794):在android.os.Handler.handleCallback(Handler.java:587)
01-09 13:32:04.086:E / AndroidRuntime(1794):在android.os.Handler.dispatchMessage(Handler.java:92)
01-09 13:32:04.086:E / AndroidRuntime(1794):在android.os.Looper.loop(Looper.java:123)
01-09 13:32:04.086:E / AndroidRuntime(1794):在android.app.ActivityThread.main(ActivityThread.java:3701)
01-09 13:32:04.086:E / AndroidRuntime(1794):在java.lang.reflect.Method.invokeNative(本机方法)
01-09 13:32:04.086:E / AndroidRuntime(1794):在java.lang.reflect.Method.invoke(Method.java:507)
01-09 13:32:04.086:E / AndroidRuntime(1794):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:862)
01-09 13:32:04.086:E / AndroidRuntime(1794):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
01-09 13:32:04.086:E / AndroidRuntime(1794):在dalvik.system.NativeStart.main(本机方法)


解决方案

这是因为<一个href=\"http://developer.android.com/reference/android/location/LocationManager.html#getLastKnownLocation%28java.lang.String%29\"相对=nofollow> LocationManager.getLastKnownLocation()可以返回null。

在您的code,你要求GPS提供商更新您的deivce的位置。
但是,当设备的GPS是关闭的,该方法返回null。

如果任何请求尚未通过的LocationManager处理,<一个href=\"http://developer.android.com/reference/android/location/LocationManager.html#getLastKnownLocation%28java.lang.String%29\"相对=nofollow> LocationManager.getLastKnownLocation()返回空值也是如此。
由于的LocationManager没有一个最后一次正确的位置的价值。

(你可以用下面的ADB命令检查的LocationManager的状态)

  $亚行外壳dumpsys位置

因此​​,如果你想与你的code段获得成功,您的设备的GPS是ON和的LocationManager有一个最后一次正确的位置

好吧,我用<一个href=\"http://developer.android.com/reference/android/location/LocationManager.html#getLastKnownLocation%28java.lang.String%29\"相对=nofollow> LocationManager.getLastKnownLocation()方法为好,但略有不同。

我只检查是否存在与<任何一个最后一次正确位置的价值href=\"http://developer.android.com/reference/android/location/LocationManager.html#getLastKnownLocation%28java.lang.String%29\"相对=nofollow> LocationManager.getLastKnownLocation()方法。因为这是解决当前位置值最快的方法。
然而,如果它返回null,然后通过<一个请求位置更新href=\"http://developer.android.com/reference/android/location/LocationManager.html#requestLocationUpdates%28long,%20float,%20android.location.Criteria,%20android.app.PendingIntent%29\"相对=nofollow> requestLocationUpdates 。

 位置位置= lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
如果(位置== NULL){
    //请求位置更新!
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,侦听);
}

也有一些工艺解决设备的位置,我认为这会是巨大的答案。
相反,我会链接此<一个href=\"http://www.google.com/events/io/2011/sessions/android-protips-advanced-topics-for-expert-android-app-developers.html\"相对=nofollow>视频大约是位置很好的解释。 (和其他更多的技巧!)

I've got an app that uses MapView and I print out the user's latitude, longitude and horizontal accuracy to some labels. This all works fine on my HTC Wildfire, but on my SE Xperia the app crashes whenever I try to touch Location.getLatitude(), Location.getLongitude() or Location.getAccuracy().

I've got a hunch that it could be the GPS on the Xperia being so slow the location manager hasn't obtained its coordinates when I'm polling for lat, long and accuracy - but how can i safeguard against this?

Here's the snippet:

        mapView.setBuiltInZoomControls(false);
        mc = mapView.getController();
        int maxLat = (int) (34.07687 * 1E6);
        int maxLon = (int) (-118.438239 * 1E6);
        int minLat = (int) (34.06489 * 1E6);
        int minLon = (int) (-118.452358 * 1E6);
        List <Overlay> overlays = mapView.getOverlays();

        MyLocationOverlay myLocationOverlay = new MyLocationOverlay(this, mapView);
        myLocationOverlay.enableMyLocation();
        overlays.add(myLocationOverlay);

        mc.zoomToSpan(Math.abs(maxLat - minLat), Math.abs(maxLon - minLon));

        lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);    
        Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); 



        GeoPoint gp = new GeoPoint((int)(location.getLatitude() * 1E6), (int)(location.getLongitude() * 1E6));

LOGCAT OUTPUT:

01-09 13:32:04.086: W/dalvikvm(1794): threadid=1: thread exiting with uncaught exception (group=0x2aac8560)
01-09 13:32:04.086: E/AndroidRuntime(1794): FATAL EXCEPTION: main
01-09 13:32:04.086: E/AndroidRuntime(1794): java.lang.NullPointerException
01-09 13:32:04.086: E/AndroidRuntime(1794):     at no.tibeapp.sno.UlovligeGarnActivity$1.run(UlovligeGarnActivity.java:180)
01-09 13:32:04.086: E/AndroidRuntime(1794):     at android.os.Handler.handleCallback(Handler.java:587)
01-09 13:32:04.086: E/AndroidRuntime(1794):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-09 13:32:04.086: E/AndroidRuntime(1794):     at android.os.Looper.loop(Looper.java:123)
01-09 13:32:04.086: E/AndroidRuntime(1794):     at android.app.ActivityThread.main(ActivityThread.java:3701)
01-09 13:32:04.086: E/AndroidRuntime(1794):     at java.lang.reflect.Method.invokeNative(Native Method)
01-09 13:32:04.086: E/AndroidRuntime(1794):     at java.lang.reflect.Method.invoke(Method.java:507)
01-09 13:32:04.086: E/AndroidRuntime(1794):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:862)
01-09 13:32:04.086: E/AndroidRuntime(1794):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
01-09 13:32:04.086: E/AndroidRuntime(1794):     at dalvik.system.NativeStart.main(Native Method)

解决方案

That's because LocationManager.getLastKnownLocation() can return null.

In your code, you requested GPS provider to update your deivce's location. But when device's GPS is off, that method return null.

And if any requests hasn't been processed through LocationManager, LocationManager.getLastKnownLocation() return null value as well. Because LocationManager doesn't have a "Last Known Locations" value.

(You can check LocationManager's state with following adb command)

$adb shell dumpsys location

Therefore, if you want to get success with your code snippet, your device's GPS is ON and LocationManager has a "Last Known Locations"

Well, I use LocationManager.getLastKnownLocation() method as well, but slightly different .

I just check if there is any "Last Known Locations" value with LocationManager.getLastKnownLocation() method. Because that is fastest way to resolve current location value. However, If it return null, then request location update through requestLocationUpdates.

Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
if (location == null) {
    // request location update!!
    lm.requestLocationUpdates (LocationManager.GPS_PROVIDER, 0, 0, listener);
}

There is also some technic resolve device's location, I think that's gonna be huge answer. Instead, I will link this video which is great explanation about location. (and other more tips!!)

这篇关于定位方法调用崩溃在某些设备上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆