getLastKnownLocation返回NULL

r0b*_*077 15 gps android

我刚刚开始工作,但是当我关闭我的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();
    }
Run Code Online (Sandbox Code Playgroud)

然后需要调用此方法

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();
    }
Run Code Online (Sandbox Code Playgroud)

你可以看到我在那里有两个吐司,他们用实际的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;
        }
    }
Run Code Online (Sandbox Code Playgroud)

我只是不明白为什么当我尝试检索位置,这似乎已在onLocationChanged方法中检索,它只是出现了一个nullpointer.

Tro*_*ske 31

我只是改变了

locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER) to 
locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
Run Code Online (Sandbox Code Playgroud)

这解决了我的解决方案.完整代码段:

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); 
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.

  • 它适用于我,但GPS_PROVIDER和PASSIVE_PROVIDER之间的区别是什么? (2认同)
  • 它不起作用.... Location location = locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER); Log.d(TAG, "OnRes:lastknown:" + locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER)); Log.d(TAG, "OnRes:lastknown:" + location); 在 logcat 中给我 null (2认同)

tyc*_*czj 7

当你不应该将新的和旧的位置API混合在一起时.

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

mLocationClient.getLastLocation();
Run Code Online (Sandbox Code Playgroud)

一旦定位服务连接.

阅读如何使用新的位置API

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