Android: Google play games services connection error ( java.lang.IllegalStateException: GoogleApiClient must be connected.)

前端 未结 2 2135
梦谈多话
梦谈多话 2020-12-07 01:37

I\'ve programmed a game for android, everything works fine, but now I want my app to have Google play Games services (leaderboards and achievements). I used

相关标签:
2条回答
  • 2020-12-07 02:07

    Check the part where you are getting ApiClient i.e. getApiClient(). Write the code below to see if GoogleApiClient is Connected or not.

       GoogleApiClient mGoogleApiClient;
        if(mGoogleApiClient.isConnected()){
         // good
        }else{
         //connect it
         mGoogleApiClient.connect(GoogleApiClient.SIGN_IN_MODE_OPTIONAL);
        }
    
    0 讨论(0)
  • 2020-12-07 02:20

    According to the official documentation, "Before any operation is executed, the GoogleApiClient must be connected"

    When the user in not connected(signed in) and clicks to show leaderboards or achievements, it results in the exception thrown. Modify your code for launching the leaderboard like this:

    } else if(view.getId()==R.id.highscorebutton) {
         if (isSignedIn()) 
         startActivityForResult(Games.Leaderboards.getLeaderboardIntent(getApiClient(), getString(R.string.the_best_players)), REQUEST_LEADERBOARD);
         else showAlert("Please sign in to view leaderboards");
    
    }
    

    Use the same logic for showing achievements:

     if (isSignedIn()) 
     startActivityForResult(Games.Achievements.getAchievementsIntent(getApiClient()), REQUEST_ACHIEVEMENT);
     else showAlert("Please sign in to view achievements");
    
    0 讨论(0)
提交回复
热议问题