java.lang.IllegalStateException:GoogleApiClient尚未连接

问题描述:

我试图通过使用Google Play游戏服务为我的游戏添加成就。我的游戏中负责提供成就的活动已经扩展了BaseGameActivity,并且在必须提供成就时调用beginUserInitiatedSignIn,因此用户必须登录,但在我解锁用户成就时,我保留得到java.lang.IllegalStateException:GoogleApiClient尚未连接。谁能告诉我我做错了什么?这里是负责解锁成就的代码(它在BaseGameUtils中扩展BaseGameActivity的类中):

I'm trying to add achievements to my game by using google play game services. The activity in my game that is responsible for giving the achievement already extends BaseGameActivity and it calls the beginUserInitiatedSignIn when it has to give the achievement, so the user must be signing in, but at the time I unlock the achievement for the user, I keep getting "java.lang.IllegalStateException: GoogleApiClient is not connected yet". Can anyone tell me what I am doing wrong? Here's the code responsible for unlocking the achievement(it's in the class that extends BaseGameActivity, from BaseGameUtils):

private void darConquistaDerrubouArvore(int numeroDeAcertos) {
     // start the asynchronous sign in flow
    mSignInClicked = true;
    mGoogleApiClient.connect();
    if(numeroDeAcertos <= 40)
    {
        try
        {
                beginUserInitiatedSignIn();
                Games.Achievements.unlock(gameHelper.getApiClient(), "CgkIs_27xcoSEAIQAQ");
                Log.i("TelaModoCasual", "usuário não está logado");
                this.onSignInFailed();

        }
        catch(Exception exc)
        {
            exc.printStackTrace();
            this.onSignInFailed();
        }
    }

}


查看基本示例 https://github.com/playgameservices/android-basic-samples/tree/master/BasicSamples/TypeANumber 。 Google API的连接流程是异步的,因此您无法解锁成就。有一个回调函数onConnected(),一旦建立连接就会调用它。在该方法中,您可以解锁成就。

Take a look at the basic sample https://github.com/playgameservices/android-basic-samples/tree/master/BasicSamples/TypeANumber. The connection flow for the Google APIs is asynchronous, so you can't unlock the achievement as you have it. There is a callback onConnected() which is called once the connection is established. In that method you can unlock the achievements.

api客户端的文档位于 http://developer.android.com/google/auth/api-client.html

The documentation for the api client is at http://developer.android.com/google/auth/api-client.html