2012-03-31 79 views
1

我有一個應用程序,使用MapView,我打印出用戶的緯度,經度和水平精度的一些標籤。這一切都適用於我的HTC Wildfire,但在我的SE Xperia上,只要我嘗試觸摸Location.getLatitude(),Location.getLongitude()Location.getAccuracy(),應用程序就會崩潰。位置方法調用在某些設備上崩潰

我有一種預感,它可能是Xperia上的GPS速度太慢了,當我輪詢拉特,長度和準確度時,位置管理器還沒有獲得它的座標 - 但是我怎麼能保護這個?

這裏的片段:

 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的輸出:

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) 
+0

這是什麼在logcat中打印時崩潰的代碼與此應對? – 2012-03-31 08:14:54

+0

@Alex我剛剛更新了Q與logcat輸出 - NullPointerException它似乎 – PinkFloydRocks 2012-03-31 08:16:53

回答

3

這是因爲LocationManager.getLastKnownLocation()可以返回null。

在您的代碼中,您要求GPS提供商更新您的設備位置。 但是當設備的GPS關閉時,該方法返回null。

如果還沒有通過LocationManager處理任何請求,則LocationManager.getLastKnownLocation()也會返回空值。 因爲LocationManager沒有「Last Known Locations」值。

(你可以用下面的ADB命令檢查的LocationManager的狀態)

$adb shell dumpsys location 

因此,如果你想與您的代碼段獲得成功,您的設備的GPS設置爲ON,的LocationManager有一個「最後已知位置」

那麼,我也使用LocationManager.getLastKnownLocation()方法,但略有不同。

我只是用LocationManager.getLastKnownLocation()方法檢查是否有任何「Last Known Locations」值。因爲這是解決當前位置價值的最快方法。 但是,如果它返回null,則通過requestLocationUpdates請求位置更新。

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

還有一些技術解決設備的位置,我認爲這將是一個巨大的答案。 相反,我會鏈接這個video這是關於位置的很好的解釋。 (和其他更多的技巧!)

1

如果一直沒有修復的話,那麼該行

Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); 

位置將是無效後。因此,你需要通過封閉需要與

if (location != null){ 
    // your code 
} 
+0

是的 - 我發現了這個問題後不久我發佈的問題,並會自己回答它,但必須等待7個小時才能。儘管謝謝你的回答! – PinkFloydRocks 2012-04-11 11:14:06

1

位置或試試這個

private class JavaScriptInterface { // use in javascript 
    public double getLatitude() { 
     try{ 
      return mostRecentLocation.getLatitude(); 
     }catch(Exception e){ //some devive without last location 
      return 25.046254;//default set Taipei Train Station (City Land Mark) 
     } 
    } 
    public double getLongitude() { 
     try{ 
      return mostRecentLocation.getLongitude(); 
     }catch(Exception e){ //some devive without last location 
      return 121.51752;//default set Taipei Train Station (City Land Mark) 
     } 
    } 
} 
相關問題