Android:GoogleAppClient尚未连接?

Android:GoogleAppClient尚未连接?,android,location,google-api-client,Android,Location,Google Api Client,更新用户的当前位置 我在我的项目中实施以下方法: GoogleAppClient.ConnectionCallbacks, GoogleAppClient.OnConnectionFailedListener 执行项目时,将发生以下错误: java.lang.IllegalStateException: GoogleApiClient is not connected yet. 代码中有一个错误: LocationServices.FusedLocationApi.requestLocatio

更新用户的当前位置 我在我的项目中实施以下方法:

GoogleAppClient.ConnectionCallbacks, GoogleAppClient.OnConnectionFailedListener

执行项目时,将发生以下错误:

java.lang.IllegalStateException: GoogleApiClient is not connected yet.
代码中有一个错误:

LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
已经提出了许多解决方案,如和 但不能解决任何问题

请帮忙解决它

onCreate方法中的我的代码:

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        FirebaseCrash.report(new Exception("Exceptin"));

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            checkLocationPermission();
        }

        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
        mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        mGoogleClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
        mLocationRequest = LocationRequest.create()
                .setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY)
                .setInterval(60*1000) 
                .setFastestInterval(1*1000);}
并且在4月1日:

@Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        buildGoogleApiClient();
        mMap.setTrafficEnabled(true);
        mMap.setMyLocationEnabled(true);}
这是:

   protected synchronized void buildGoogleApiClient() {
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
        mGoogleApiClient.connect();
    }

 @Override
    public void onConnected(@Nullable Bundle bundle) {

 if (servicesAvailable()) {
            // Get best last location measurement meeting criteria
            mLastLocation = bestLastKnownLocation(MIN_LAST_READ_ACCURACY, FIVE_MIN);

            if (null == mLastLocation
                    || mLastLocation.getAccuracy() > MIN_LAST_READ_ACCURACY
                    || mLastLocation.getTime() < System.currentTimeMillis() - TWO_MIN) {

                LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);

                // Schedule a runnable to unregister location listeners
                Executors.newScheduledThreadPool(1).schedule(new Runnable() {

                    @Override
                    public void run() {
                        LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, MainActivity.this);//This Line Return Error
                    }

                }, ONE_MIN, TimeUnit.MILLISECONDS);
            }
        }else{
            handleNewLocation(mLastLocation);
        }

    }
protected synchronized void buildGoogleAppClient()受保护{
mgoogleapclient=新的Googleapclient.Builder(此)
.addConnectionCallbacks(此)
.addOnConnectionFailedListener(此)
.addApi(LocationServices.API)
.build();
mGoogleApiClient.connect();
}
@凌驾
未连接的公共无效(@Nullable Bundle){
if(servicesAvailable()){
//获得满足标准的最佳最后位置测量值
mLastLocation=BESTLASTKNOWNLLOCATION(最小上次读取精度,五分钟);
如果(null==mLastLocation
||mLastLocation.getAccuracy()>最小上次读取精度
||mLastLocation.getTime()

感谢您的帮助:)

这是对我有效的完整实现

    public class MainActivity extends AppCompatActivity implements
    GoogleApiClient.OnConnectionFailedListener,
    GoogleApiClient.ConnectionCallbacks,
    LocationListener {


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

        buildGoogleApiClient();
    }

    @Override
    public void onConnected(@Nullable Bundle bundle) {
        Log.d(TAG, "Connection established. Fetching location ..");
        mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(1000);
        mLocationRequest.setFastestInterval(1000);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);

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

    @Override
    public void onLocationChanged(Location location) {

        Log.d("Lat: " + location.getLatitude() + "Lng : " + location.getLongitude());    
        LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
    }

    public synchronized void buildGoogleApiClient() {
        mGoogleApiClient = new GoogleApiClient.Builder(getBaseContext())
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();
        mGoogleApiClient.connect();
    }

            @Override
        public void onStart(){
            super.onStart();
            if(this.googleApiClient != null){
                    this.googleApiClient.connect();
            }
        }

    @Override
    public void onPause() {
        super.onPause();
        //stop location updates when Activity is no longer active
        if (mGoogleApiClient != null) {
            LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
        }
    }
}

在onCreate中,在没有客户端的情况下调用
getmapsync()


您需要在
onCreate
中调用
mGoogleClient.connect()
,然后将
mapFragment.getmapsync(this)
移动到
onConnected
,解决了我的问题,因为我在我的项目中使用了闪屏,将此代码放入闪屏活动中:

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

感谢您快速响应,但问题尚未解决。异常是否仍然发生?您必须首先在oncreate()中创建
mGoogleClient
。是的,mGoogleClient在oncreate()中,但在onStart()中发生nullexception。。。