2

我的问题是我无法使用 LocationManager 类和 LocationListener 获取 GPS 坐标。这是我在 OnCreate() 中调用的代码:

    locMgr = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    currentLocListener = new MyLocationListener();
    locMgr.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, currentLocListener);
    locMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, currentLocListener);

我有一个类 MyLocationListener 实现类 LocationListener

public class MyLocationListener implements LocationListener {

@Override
public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub
    String coordinates[] = {""+location.getLatitude(),""+location.getLongitude()};
    double lat = Double.parseDouble(coordinates[0]);
    double lng = Double.parseDouble(coordinates[1]);
    GeoPoint p = new GeoPoint((int)(lat*1E6), (int)(lng*1E6));
    mc.animateTo(p);
    mc.setZoom(19);

    MyMapOverlays marker = new MyMapOverlays(p);
    List<Overlay> listofOverLays = mapview.getOverlays();
    listofOverLays.clear();
    listofOverLays.add(marker);
    mapview.invalidate();
}

@Override
public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub
    Toast.makeText(getApplicationContext(), "GPS is disabled", Toast.LENGTH_SHORT).show();
}

@Override
public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub
    Toast.makeText(getApplicationContext(), "GPS is Enabled", Toast.LENGTH_SHORT).show();
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub
}}

我可以通过在 DDMS 的 Emulator Control 中模拟位置来获取位置。所以我尝试在我的真实手机上进行测试。当我将它安装在手机上时,虽然我打开了 GPS 和 wifi,但它无法自动获取当前位置。

我尝试在我的真实手机上运行另一个谷歌地图应用程序,它运行良好。我不明白代码有什么问题......谁能帮助我?

LOGCAT 和调试

DalvikVM[localhost:8631]    
    Thread [<1> main] (Suspended (exception NullPointerException))  
        DashboardActivity$MyLocationListener.onLocationChanged(Location) line: 75   
        LocationManager$ListenerTransport._handleMessage(Message) line: 191 
        LocationManager$ListenerTransport.access$000(LocationManager$ListenerTransport, Message) line: 124  
        LocationManager$ListenerTransport$1.handleMessage(Message) line: 140    
        LocationManager$ListenerTransport$1(Handler).dispatchMessage(Message) line: 99  
        Looper.loop() line: 123 
        ActivityThread.main(String[]) line: 4627    
        Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
        Method.invoke(Object, Object...) line: 521  
        ZygoteInit$MethodAndArgsCaller.run() line: 868  
        ZygoteInit.main(String[]) line: 626 
        NativeStart.main(String[]) line: not available [native method]  
    Thread [<6> Binder Thread #2] (Running) 
    Thread [<5> Binder Thread #1] (Running) 
    Thread [<7> MapService] (Running)   
    Thread [<8> TrafficService] (Running)   
    Daemon Thread [<10> RefQueueWorker@org.apache.http.impl.conn.tsccm.ConnPoolByRoute@4610b970] (Running)  

第 75 行给出错误。

4

1 回答 1

2

由于您还请求 NETWORK_PROVIDER 的更新,因此您还需要包含权限:

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

我的猜测是这条线:

locMgr.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, currentLocListener);

...正在抛出 SecurityException,因此注册 GPS_PROVIDER 的以下行永远不会执行。

要进行故障排除,我建议注释掉从 NETWORK_PROVIDER 请求更新的行,然后查看 GPS 是否在设备上工作。或者,您可以尝试在清单中包含上述权限,然后检查以确保您在 onLocationChanged() 方法中收到的位置来自 GPS_PROVIDER。

另外,请尝试消除要转换为字符串数组的第一行。相反,直接为变量赋值:

double lat = location.getLatitude();
double lng = location.getLongitude();
于 2012-12-17T19:18:09.393 回答