2010-02-08 59 views
2

我想获得当前很长的经纬度。我对 LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);如何解决获取纬度或GPS空指针异常?

LocationListener myLocationListener = new CurrentLocationListener(); 
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1, 1, myLocationListener); 
    Location currentLocation = locationManager.getLastKnownLocation("gps"); 
    Latitude = currentLocation.getLatitude(); ----->>> null pointer exception here 
    Longitude = currentLocation.getLongitude(); 

public class CurrentLocationListener implements LocationListener{ 
    public void onLocationChanged(Location argLocation) { 
    } 
    public void onProviderDisabled(String provider) { 
    } 
    public void onProviderEnabled(String provider) { 
    } 
    public void onStatusChanged(String provider, int status, Bundle arg2) { 
    } 
} 

得到一个空指针我的清单文件

<uses-permission android:name="android.permission.INTERNET"></uses-permission> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission> 
+0

currentLocation显然返回空。建议你解决这个问题... – 2010-02-08 07:39:08

+0

其实我是这个领域的新手,请你指导我该怎么做才能解决这个问题? 是的,它是空.. – UMAR 2010-02-08 07:45:56

+0

你使用模拟器或实际设备? – 2010-02-08 07:58:11

回答

1

你设置清单文件正确的权限?喜欢的东西:

<uses-permission 
    android:name="android.permission.ACCESS_FINE_LOCATION" /> 
1

类明显...

Location currentLocation = locationManager.getLastKnownLocation("gps"); 

if (currentLocation != null) 
{ 
    Latitude = currentLocation.getLatitude(); 
    Longitude = currentLocation.getLongitude(); 
} 

现在,关于为什么的LocationManager返回一个空引用,而不是实际位置参考的...这是很难知道与提供的代码。可能是任何事情。

编辑:嗯,似乎它可能是你有no gps fix yet

+0

我是否需要安装别的东西来激活GPS? – UMAR 2010-02-08 08:13:54

+0

从模拟器,运行的时候,去你的DDMS透视图(这就是所谓的像这样吧?),选择“模拟器控制”选项卡,你会看到一个“定位控制”的一部分,你可以发送经/纬度位置 – ccheneson 2010-02-08 13:25:30

3

这通常是因为没有最后一个已知位置。要强制获得可以为位置更新登记和等待,或者你可以启动谷歌地图应用,获得使用菜单位置的位置 - >我的位置,返回到应用程序,并最后一个已知的位置,不应为null。

+0

你能不能请填写这是什么意思menu->我的位置,以便我可以解决这个问题? – UMAR 2010-02-17 09:17:41

+0

(触摸)谷歌地图应用程序(图标) - >(按)菜单(按钮) - >(触摸)我的位置(图标) – 2010-02-17 16:54:55

+0

thanks..it工作.. – 2014-10-10 06:43:53