nected并且我正在onCreate中创建我的GoogleAp

nected并且我正在onCreate中创建我的GoogleAp

本文介绍了GoogleApiClient尚未连接,即使已调用onConnected并且我正在onCreate中创建我的GoogleApiClient的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里看了这个Q& A:

I looked at this Q&A here: GoogleApiClient is throwing "GoogleApiClient is not connected yet" AFTER onConnected function getting called

因为它看起来与我的体验类似,但事实并非如此。该用户的问题是他们在onStart()方法中声明了他们的api客户端,我在onCreate()方法中创建了我的答案。然而,我仍然遇到同样的错误。

As it seemed to be similar to what I was experiencing but, it's not. The issue with that user was that they were declaring their api client in the onStart() method, I create mine in the onCreate() method like suggested by the answer. I am still however, getting the same error.

以下是我对这三种方法的代码:

Here is the code I have for these three methods:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    /* Butter knife creates the variables */
    ButterKnife.bind(this);

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

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

    progressBar.setVisibility(View.INVISIBLE);

    refreshImageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            getForecast();
        }
    });
}

@Override
protected void onStart() {
    super.onStart();
    mGoogleApiClient.connect();
    Log.i("Connected!!!", "WERE CONNECTED");
}

@Override
protected void onResume() {
    super.onResume();
    if (!mGoogleApiClient.isConnected()) {
        mGoogleApiClient.connect();
    }

    resumeLocationUpdates();

}
private void resumeLocationUpdates() {
    Log.i("RESUMING", "RESUMING LOCATION UPDATES");
    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, locationRequest, this);
}

这是记录器显示的内容,这让我感到困惑,因为它显示已连接但应用程序崩溃说没有连接......

This is what the logger shows, which is confusing me since it shows were connected but the app crashes saying were not connected...

-14 02:25:13.582 25885-25885/? I/Connected!!!: WERE CONNECTED
11-14 02:25:13.582 25885-25885/? I/RESUMING: RESUMING LOCATION UPDATES
11-14 02:25:13.582 25885-25885/? D/AndroidRuntime: Shutting down VM
11-14 02:25:13.583 25885-25885/? E/AndroidRuntime: FATAL EXCEPTION: main
11-14 02:25:13.583 25885-25885/? E/AndroidRuntime: Process: lpadron.me.weatherly, PID: 25885
11-14 02:25:13.583 25885-25885/? E/AndroidRuntime: java.lang.RuntimeException: Unable to resume activity {lpadron.me.weatherly/lpadron.me.weatherly.MainActivity}: java.lang.IllegalStateException: GoogleApiClient is not connected yet.


推荐答案

您的问题出在 onResume logic:

Your problem is in the onResume logic:

@Override
protected void onResume() {
    super.onResume();
    if (!mGoogleApiClient.isConnected()) {
        mGoogleApiClient.connect();
    }

    resumeLocationUpdates();

}
private void resumeLocationUpdates() {
    Log.i("RESUMING", "RESUMING LOCATION UPDATES");
    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, locationRequest, this);
}

拨打 mGoogleApiClient.connect()是异步的。它在连接完成之前返回,并且您在连接客户端之前请求位置更新。您需要将 requestLocationUpdates 调用移至 GoogleApiClient.onConnected 回调。在此活动之后,您的客户端已连接。

The call to mGoogleApiClient.connect() is asynchronous. It returns before the connect is finished, and you are requesting location updates before the client is connected. You need to move the requestLocationUpdates call to the GoogleApiClient.onConnected callback. After this event, your client is connected.

@Override
protected void onResume() {
    super.onResume();
    if (!mGoogleApiClient.isConnected()) {
        mGoogleApiClient.connect();
    }
}

@Override
public void onConnected(Bundle bundle) {
    resumeLocationUpdates();
}

这篇关于GoogleApiClient尚未连接,即使已调用onConnected并且我正在onCreate中创建我的GoogleApiClient的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 06:54