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

前端 未结 2 2142
梦谈多话
梦谈多话 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: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");
    

提交回复
热议问题