【问题标题】:Can't use an activity as an instance of LocationListener when calling requestLocationUpdates()调用 requestLocationUpdates() 时不能将活动用作 LocationListener 的实例
【发布时间】:2015-06-29 03:23:57
【问题描述】:

在我的应用程序中,连接到LocationServices.API后,在onConnected()回调中,我启动了一个位置更新请求,问题是处理进程的Activity(DataActivity)没有被识别为LocationListener的实例,它显示一个错误,并建议转换。

protected void startLocationUpdates() {
    PendingResult locationUpdatesResult = LocationServices.FusedLocationApi.requestLocationUpdates(locationGoogleApiClient, locationRequest, (com.google.android.gms.location.LocationListener) this);
}

当我将最后一个参数转换为 (com.google.android.gms.location.LocationListener) 时,IDE 允许我编译,但抛出 ClassCastException

java.lang.ClassCastException: com.example.[...].DataActivity 无法转换为 com.google.android.gms.location.LocationListener

我真的不知道为什么会这样。

这是类的声明。

public class DataActivity extends FragmentActivity implements LocationListener, ConnectionCallbacks , OnConnectionFailedListener, OnMapReadyCallback{

OnCreate():

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_data);

    //Create the GoogleLocationAPI reference
    buildGoogleLocationApiClient();

    //Create Location Request
    createLocationRequest();

    //Initialize the mapFragment
    initializeMapFragment();

    updateUI();
}

其他方法:

protected synchronized void buildGoogleLocationApiClient(){
    locationGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(LocationServices.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();
    locationGoogleApiClient.connect();
    Log.v("**INFO: ", "API CLIENT");
}

protected static void createLocationRequest() {
    locationRequest = new LocationRequest();
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    locationRequest.setInterval(10000);
    locationRequest.setFastestInterval(5000);
    Log.i("**INFO: ", "LocationRequest created");
}

protected void initializeMapFragment(){
    //Initialize the mapFragment
    mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
    //When mapFragment is ready...
    mapFragment.getMapAsync(this);
}

所以当建立连接(OnConnected)时,我调用这个方法,当错误弹出时。

protected void startLocationUpdates() {
    PendingResult locationUpdatesResult = LocationServices.FusedLocationApi.requestLocationUpdates(locationGoogleApiClient, locationRequest, this);
    locationUpdatesResult.await(); 
}

【问题讨论】:

    标签: android android-studio localization location-services


    【解决方案1】:

    确保您的 DataActivity 实现 com.google.android.gms.location.LocationListener 而不是 android.location.LocationListener
    您可能错误地导入了android.location.LocationListener

    【讨论】:

    • 谢谢!!,这就是问题所在;)。我仍然不能投票,(声誉)但我真的很感激。
    • 即使添加 com.google.android.gms.location.LocationListener 对我有用...还有其他建议吗?
    • @Sharath,您可能错过了 gradle 文件中的依赖项“compile 'com.google.android.gms:play-services-location:7.5.0'”
    • @Gero 我导入了 com.google.android.gms.location.LocationListener 但它无法在我的片段上运行,仍然崩溃
    【解决方案2】:

    而不是public class DataActivity extends FragmentActivity implements LocationListener, ConnectionCallbacks , OnConnectionFailedListener, OnMapReadyCallback{

    public class DataActivity extends FragmentActivity implements **com.google.android.gms.location.LocationListener**, ConnectionCallbacks , OnConnectionFailedListener, OnMapReadyCallback{
    

    之后你必须实现它的所有方法

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-04
      • 1970-01-01
      • 1970-01-01
      • 2012-06-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多