错误fusedLocationApi requestLocationUpdates()非活动类错误

时间:2016-03-14 14:13:30

标签: android location updates locationlistener fusedlocationproviderapi

嗨,我是stackoverflow的新手。我希望有人知道在Android中如何使用FusedLocationApi调用方法requestLocationUpdates。

我正在尝试调用非活动类,该类实现了nedded以请求位置更新的类:          LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,mLocationRequest,this);

但它显示消息“无法解析方法requestLocationUpdates(...)”

 /**
 * Requests location updates from the FusedLocationApi.
 */
protected void startLocationUpdates() {
  LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}

我使用的非活动类实现了ConnectionCallbacks,OnConnectionFailedListener和LocationListener,并扩展了我的活动类。

public class OnMapGps extends OnActivity implements
    GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener,
    LocationListener {

    ...
    ...
}

显示的错误是:

Error:(306, 42) error: no suitable method found for requestLocationUpdates(GoogleApiClient,LocationRequest,OnMapGps)
method FusedLocationProviderApi.requestLocationUpdates(GoogleApiClient,LocationRequest,LocationListener)
is not applecable (argument mismatch; OnMapGps cannot be converted to LocationListener) 

2 个答案:

答案 0 :(得分:2)

我找到了解决方案,似乎我有错误的导入LocationListener。我有

import android.location.LocationListener;

正确的是

import com.google.android.gms.location.LocationListener;

我认为我必须要求自己找到解决方案。 :P

答案 1 :(得分:1)

我通过将'this'转换为'LocationListener'对象

来解决了这个错误
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, (LocationListener) this);