2

当我尝试在三星 Galaxy s3 上获取我的位置时,我的应用程序崩溃了。它在我测试过的其他 3 部手机上完美运行。我在三星 Galaxy s3 上只有远程测试人员,所以我无法收到错误消息。这是我的代码:

    Location finalLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    locationManager.removeUpdates(locationListenerGps);
    locationManager.removeUpdates(locationListenerNetwork);

    Location net_loc=null, gps_loc=null;
    if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
        gps_loc=locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    if(locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
        net_loc=locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

    //if there are both values use the latest one
    if(gps_loc!=null && net_loc!=null){
        if(gps_loc.getTime()>net_loc.getTime())
            finalLocation = gps_loc;
        else
            finalLocation = net_loc;
    }

    if(gps_loc!=null){
        finalLocation = gps_loc;

    }
    if(net_loc!=null){
        finalLocation = net_loc;

    }
4

2 回答 2

1

我认为这是 4.0.4 版本的“功能”,检查(getLastKnownLocation 在三星 Galaxy S3 4.0.4 上不起作用)

我有同样的问题,当我升级到 4.1 时它就消失了。

您可能必须抓住两者都为空的情况,我发现 PASSIVE_PROVIDER 有效。

   if ( gps_loc == null && net_loc == null ) { // only S3 gets this....
      finalLocation=  locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
   }
   if ( finalLocation == null ) panic....
于 2012-12-30T07:46:30.477 回答
0

如果没有 logcat,很难说出确切的问题,但 getLastKnownLocation 在某些情况下可能会返回 null(不太可能但可能)。如果是这种情况,很容易看出这会导致崩溃。也许尝试添加一些代码来检查 getLastKnownLocation 是否返回了一个值,如果没有,请使用虚拟值或“暂停”您的操作并自己获取位置。

http://developer.android.com/reference/android/location/LocationManager.html#getLastKnownLocation(java.lang.String )

于 2012-12-30T08:02:52.687 回答