简体   繁体   中英

java.lang.IllegalStateException: GoogleApiClient is not connected yet

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();
        }
    }

}

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM