LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient,this); 尝试投射为com.google.android.gms.location.LocationListener)

Dfr*_*373 9 android location casting google-maps-api-3

我在Android上设置了基于位置的地图,但是当我尝试并调用时: LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);

Android工作室试图让我把它作为

LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, (com.google.android.gms.location.LocationListener) this);
Run Code Online (Sandbox Code Playgroud)

但是,这会导致应用程序在调用时崩溃.

我的应用程序有以下位置代码:

import android.content.IntentSender;
import android.location.Location;
import android.location.LocationListener;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.util.Log;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.location.LocationServices;

import java.util.List;

import model.SiteModel;
import model.SqlHelper;

public class FindNearestLocationMap extends FragmentActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {

    private GoogleMap mMap; // Might be null if Google Play services APK is not available.

    private GoogleApiClient mGoogleApiClient;

    public static final String TAG = FindNearestLocationMap.class.getSimpleName();

    private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000;

    private LocationRequest mLocationRequest;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_find_nearest_location_map);

        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();

        mLocationRequest = LocationRequest.create()
                .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
                .setInterval(10 * 1000)        // 10 seconds, in milliseconds
                .setFastestInterval(1 * 1000); // 1 second, in milliseconds

        setUpMapIfNeeded();
    }

    @Override
    protected void onResume() {
        super.onResume();
        setUpMapIfNeeded();
        mGoogleApiClient.connect();
    }

    @Override
    protected void onPause() { //////ERROR IS HERE////////
        super.onPause();
        if (mGoogleApiClient.isConnected()) {
            LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, (com.google.android.gms.location.LocationListener) this);
            mGoogleApiClient.disconnect();
        }
    }
....
Run Code Online (Sandbox Code Playgroud)

我拥有Classes所需的所有实现方法,并且我能够运行该程序.只有当onPause被称为我应该做什么的想法时它才会中断?

谢谢.

ian*_*ake 19

你正在使用错误的导入LocationListener.你有:

import android.location.LocationListener;
Run Code Online (Sandbox Code Playgroud)

这是遗产LocationListener而不是融合位置提供者的倾听者:

import com.google.android.gms.location.LocationListener;
Run Code Online (Sandbox Code Playgroud)