Home>

I installed a Google Play Game Services leaderboard on a game created using Android Studio
If i call the leaderboard, it will fall.

The state is
All registrations have been completed on the Google console management screen.
When the game is launched, Google Play Game is connected without any problems.
It falls in startActivityForResult that calls the leaderboard.

There are many people who have the same problem, but I haven't found a solution.
I would appreciate your advice.

Environment)
Android Studio 3.0.1
PC Mac

app/build.gradle

dependencies {
    compile 'com.google.android.gms: play-services-games: 10.0.0'
}


QuizActivity.java

public class QuizeActivity extends AppCompatActivity implements View.OnClickListener,
        GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
    private GoogleApiClient mGoogleApiClient;
    private boolean mIntentInProgress;
    private int REQUEST_CODE = 1;
    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView (R.layout.activity_quize);
        // GoogleApiClient initial setting
        mGoogleApiClient = new GoogleApiClient.Builder (this)
                .addConnectionCallbacks (this)
                .addOnConnectionFailedListener (this)
                .addApi (Games.API)
                .addScope (Games.SCOPE_GAMES)
                .build ();
    @Override
    protected void onStart () {
        super.onStart ();
        // Connect every time the screen is displayed
        mGoogleApiClient.connect ();
    }
    @Override
    protected void onActivityResult (int requestCode, int resultCode, Intent data) {
        super.onActivityResult (requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE) {
            mIntentInProgress = false;
            if (resultCode! = RESULT_OK) {
                return;
            }
            if (! mGoogleApiClient.isConnected ()) {
                mGoogleApiClient.reconnect ();
            }
        }
    }
    // behavior when the next button is pressed
    public void goNext (View v) {
        if (quizeNum<quizeList.size ()-1) {
// if (quizeNum<5-1) {
            if (resultNum) {
                quizeList = new ArrayList<>();
                quizeNum ++;
                hazureList = new ArrayList<>();
                quizeGet ();
                judgDisplay (2);
                numberDisplay ();
                buttonEnabled (true, true, true, false);
                buttonDisplay ();
                descriDisplay ();v.invalidate ();
            } else {
                resultDisplay ();
                if (mGoogleApiClient.isConnected ()) {
                    Games.Leaderboards.submitScore (mGoogleApiClient, String.valueOf (R.string.leaderboard_id), quizeNum);
                    switch (quizeNum) {
                        case 50:
                            Games.Achievements.unlock (mGoogleApiClient, String.valueOf (R.string.achievements_id50));
                            break;
                }
                } else {
                   mGoogleApiClient.connect ();
                    Fall here. ↓
StartActivityForResult (Games.Achievements.getAchievementsIntent (mGoogleApiClient),
                            1);
                    startActivityForResult (Games.Leaderboards.getLeaderboardIntent (mGoogleApiClient,
                            getString (R.string.leaderboard_id)), REQUEST_CODE);
                }
            }
        } else {
            // Put the last message here
            lastDialog ();
            SlideManager.saveInt (getApplicationContext (), "highscore", quizeNum + 1);
        }
    }
    @Override
    public void onConnected (@Nullable Bundle bundle) {
        // Get Player information
        Player player = Games.Players.getCurrentPlayer (mGoogleApiClient);
        String displayName = "???";
        if (player! = null) {
            displayName = player.getDisplayName ();
        }
        Toast.makeText (getApplicationContext (), String.format ("Logging in with% s", displayName),
                Toast.LENGTH_SHORT) .show ();
    }
    @Override
    public void onConnectionSuspended (int i) {
        mGoogleApiClient.connect ();
    }
    @Override
    public void onConnectionFailed (@NonNull ConnectionResult connectionResult) {
        int errorCode = connectionResult.getErrorCode ();
        if (errorCode == ConnectionResult.SIGN_IN_REQUIRED
                &&! mIntentInProgress&&connectionResult.hasResolution ()) {
            try {
                mIntentInProgress = true;
                connectionResult.startResolutionForResult (this, 100);
            } catch (IntentSender.SendIntentException e) {
                mIntentInProgress = false;
                mGoogleApiClient.connect ();
            }
        }
    }
}


error message

FATAL EXCEPTION: main Process: com ○○ PID: 31343
                                        java.lang.IllegalStateException: GoogleApiClient must be connected.
                                        at com.google.android.gms.common.internal.zzac.zza (Unknown Source)
                                        at com.google.android.gms.games.Games.zzb (Unknown Source)
                                        at com.google.android.gms.games.Games.zzi (Unknown Source)
                                        at com.google.android.gms.games.internal.api.AchievementsImpl.getAchievementsIntent (Unknown Source)
                                        at com.XX.QuizeActivity.goNext (QuizeActivity.java:289)
                                        at com.XX.QuizeActivity.onClick (QuizeActivity.java:469)
  • Answer # 1

    In error text
    java.lang.IllegalStateException: GoogleApiClient must be connected.
    Please check if you are connected to GoogleApiClient.

    If you are unable to connect, you may not have successfully created an instance of mGoogleApiClient.

    As you may have seen, implementation of Google Play Game Services (How to open the leaderboard) ← You may want to publish it to the store as shown here (sorry if it has already been done).