LocationServices.FusedLocationApi.requestLocationUpdates未设置位置

时间:2015-02-21 17:18:03

标签: java android

我正在尝试使用GoogleAPIClient进行实时位置更新。虽然GoogleAPIClient已连接但位置对象为null,但它抱怨location.getProvider()为空。有人可以帮忙吗?

这是我的代码:

public void onConnected(Bundle connectionHint) {
    Log.i(TAG, "Connected to GoogleApiClient " + myGoogleApiClient.isConnected());

    myCurrentLocation = LocationServices.FusedLocationApi.getLastLocation(myGoogleApiClient);
    // Debug - At this point myCurrentLocation  is NULL
        if (myCurrentLocation == null) {
            Log.i(TAG, "myCurrentLocation is : " + myCurrentLocation);
            // Debug - myCurrentLocation  is STILL NULL
            LocationServices.FusedLocationApi.requestLocationUpdates(
                    myGoogleApiClient, myLocationRequest, this);
            // Debug - app crashes with stack trace below at this point
            Log.i(TAG, "Doesnt't reach here : " + myCurrentLocation);
        }

        myLastUpdateTime = DateFormat.getTimeInstance().format(new Date());
        updateUILayout();

}

    protected synchronized void buildGoogleApiClient() {
    Log.i(TAG, "Building GoogleApiClient");
    myGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();
    Log.i(TAG, "myGoogleApiClient is :" + myGoogleApiClient.isConnected());
    createLocationRequest();
}

protected void createLocationRequest() {
    myLocationRequest = new LocationRequest();
    myLocationRequest.setInterval(10000);
    myLocationRequest.setFastestInterval(5000);
    myLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}

我发现调试后的主要问题是:

 LocationServices.FusedLocationApi.requestLocationUpdates(
                    myGoogleApiClient, myLocationRequest, this);

这不是设置myCurrentLocation。它始终是NULL。在ADB中,logcat说:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.location.Location.getProvider()' on a null object reference
        at com.adrenalin.myfirstapp.MyActivity.onConnected(MyActivity.java:130)
        at com.google.android.gms.internal.jm.f(Unknown Source)
        at com.google.android.gms.common.api.c.gJ(Unknown Source)
        at com.google.android.gms.common.api.c.d(Unknown Source)
        at com.google.android.gms.common.api.c$2.onConnected(Unknown Source)
        at com.google.android.gms.internal.jm.f(Unknown Source)
        at com.google.android.gms.internal.jm.dU(Unknown Source)
        at com.google.android.gms.internal.jl$h.b(Unknown Source)

3 个答案:

答案 0 :(得分:1)

实施时

LocationServices.FusedLocationApi.requestLocationUpdates(
                    myGoogleApiClient, myLocationRequest, this);

您需要覆盖onLocationChanged的回调,以便它知道在更新时该如何处理该位置。

试试这个:

@Override
public void onLocationChanged(Location location) {
    myCurrentLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
}

答案 1 :(得分:1)

在我的情况下,我使用片段类,是否有任何解决方法在LocationServices.FusedLocationApi.requestLocationUpdates()上使用 this

  @Override
    public void onLocationChanged(Location location) {
        mLastLocation = location;
        displayLocation();
    }

    private void startLocationUpdates() {
        if(ActivityCompat.checkSelfPermission( getActivity().getApplicationContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
                ActivityCompat.checkSelfPermission( getActivity().getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED     )
        {
            return;
        }

        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,mLastLocation,this);
    }

答案 2 :(得分:0)

如果您获得Null localion

,请使用以下方法
if (myCurrentLocation == null) { 
   LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
} else {
    //your location code here...
}

并覆盖以下方法重试位置

@Override
public void onLocationChanged(Location location) {
   //your location code here...
}