16

我刚刚开始工作,但是当我关闭我的 Wifi 以查看是否可以使用我的 GPS 获得更准确的位置时,它现在给我带来了一个NullPointer错误,我不明白为什么。

下面的代码首先被调用;

 public void onConnected(Bundle dataBundle) {
        connected = true;
        //Request an update from the location client to get current Location details
        mLocationClient.requestLocationUpdates(mLocationRequest,this);
        Toast.makeText(this, "Connected", Toast.LENGTH_SHORT).show();
    }

然后需要调用此方法

public void onLocationChanged(android.location.Location location) {
        // Report to the UI that the location was updated
        String msg = "Updated Location: " +
                Double.toString(location.getLatitude()) + "," +
                Double.toString(location.getLongitude());
        if(tmp != location.getLatitude())
        {
            Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
        }
        tmp = location.getLatitude();
    }

您可以看到我在那里有 2 个祝酒词,它们以实际的 GPS 坐标返回,但是当我调用下面的代码时,它在新的位置线上崩溃了getLastKnownLocation

public String getLocation()
    {
        // Get the location manager
        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        String bestProvider = locationManager.getBestProvider(criteria, false);
        android.location.Location location = locationManager.getLastKnownLocation(bestProvider);
        Double lat,lon;
        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date date = new Date();
        try {
            lat = location.getLatitude ();
            lon = location.getLongitude ();
            Toast.makeText(this,""+lat.toString()+"-"+lon.toString(),Toast.LENGTH_SHORT).show();
            return new LatLng(lat, lon).toString();
        }
        catch (NullPointerException e){
            Toast.makeText(this,"HELL-NO",Toast.LENGTH_SHORT).show();
            Log.e("HELL-NO","n",e);
            e.printStackTrace();
            return null;
        }
    }

我只是不明白为什么当我尝试检索似乎已在onLocationChanged方法中检索到的位置时,它只是提出了一个空指针。

4

4 回答 4

33

我只是改变了

locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER) to 
locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);

这为我解决了解决方案。完整片段:

map  = ((MapFragment) getFragmentManager().
            findFragmentById(R.id.map)).getMap();

    map.setMyLocationEnabled(true);

    locationManager = (LocationManager)getSystemService
            (Context.LOCATION_SERVICE); 
    getLastLocation = locationManager.getLastKnownLocation
            (LocationManager.PASSIVE_PROVIDER);
    currentLongitude = getLastLocation.getLongitude();
    currentLatitude = getLastLocation.getLatitude();

    currentLocation = new LatLng(currentLatitude, currentLongitude); 

希望这可以帮助。

于 2014-08-11T23:10:32.550 回答
7

您不应该将新旧位置 API 混合在一起。

要获得最后一个已知位置,您所要做的就是打电话

mLocationClient.getLastLocation();

一旦连接了位置服务。

阅读如何使用新的位置 API

http://developer.android.com/training/location/retrieve-current.html#GetLocation

于 2013-10-27T18:52:57.190 回答
1

试试这个代码:

LocationManager mLocationManager;
Location myLocation = getLastKnownLocation();

private Location getLastKnownLocation() {
    mLocationManager = (LocationManager)getApplicationContext().getSystemService(LOCATION_SERVICE);
    List<String> providers = mLocationManager.getProviders(true);
    Location bestLocation = null;
    for (String provider : providers) {
        Location l = mLocationManager.getLastKnownLocation(provider);
        if (l == null) {
            continue;
        }
        if (bestLocation == null || l.getAccuracy() < bestLocation.getAccuracy()) {
            // Found best last known location: %s", l);
            bestLocation = l;
        }
    }
    return bestLocation;
}
于 2017-07-04T22:44:43.697 回答
0

我和这个斗争了很长时间。但谷歌刚刚想出了一个解决方案。

googleMap.getMyLocation();

获取 api V2 上蓝点的位置。

于 2015-12-03T08:38:30.950 回答