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

蹲街弑〆低调 提交于 2019-12-17 16:56:19

问题


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 the Google example code to log in to the Google services (no errors in the script), but every time I want to connect with my App in debug mode, I get this error:

6-29 11:48:29.391  23779-23779/com.JFKGames.theepicbutton E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.JFKGames.theepicbutton, PID: 23779
    java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=9001, result=10004, data=null} to activity {com.JFKGames.theepicbutton/com.JFKGames.theepicbutton.MainActivity}: java.lang.IllegalStateException: GoogleApiClient must be connected.
            at android.app.ActivityThread.deliverResults(ActivityThread.java:3446)
            at android.app.ActivityThread.handleSendResult(ActivityThread.java:3489)
            at android.app.ActivityThread.access$1300(ActivityThread.java:139)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1258)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5102)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.IllegalStateException: GoogleApiClient must be connected.
            at com.google.android.gms.internal.fq.a(Unknown Source)
            at com.google.android.gms.games.Games.c(Unknown Source)
            at com.google.android.gms.games.internal.api.LeaderboardsImpl.submitScore(Unknown Source)
            at com.google.android.gms.games.internal.api.LeaderboardsImpl.submitScore(Unknown Source)
            at com.JFKGames.theepicbutton.MainActivity.onActivityResult(MainActivity.java:79)
            at android.app.Activity.dispatchActivityResult(Activity.java:5446)
            at android.app.ActivityThread.deliverResults(ActivityThread.java:3442)
            at android.app.ActivityThread.handleSendResult(ActivityThread.java:3489)
            at android.app.ActivityThread.access$1300(ActivityThread.java:139)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1258)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5102)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)

And the App crashes. Here's my code for the MainActivity where I want it to connect:

public class MainActivity extends BaseGameActivity implements



GameHelper.GameHelperListener, View.OnClickListener {

    public static int REQUEST_LEADERBOARD = 1002;
    boolean mExplicitSignOut = false;
    boolean mInSignInFlow = false;

    GoogleApiClient mClient() {

        return null;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setRequestedClients(BaseGameActivity.CLIENT_GAMES | BaseGameActivity.CLIENT_APPSTATE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = (Button)findViewById(R.id.startbutton);
        button.setOnClickListener (this);
        Button highscorebutton = (Button)findViewById(R.id.highscorebutton);
        highscorebutton.setOnClickListener(this);
        findViewById(R.id.sign_in_button).setOnClickListener(this);
        findViewById(R.id.sign_out_button).setOnClickListener(this);

    }


    public void onClick(View view) {
        if(view.getId()==R.id.startbutton) {
            startActivityForResult(new Intent(this, buttonActivity.class), 1);
        } else if(view.getId()==R.id.highscorebutton) {
            startActivityForResult(Games.Leaderboards.getLeaderboardIntent(getApiClient(), getString(R.string.the_best_players)),REQUEST_LEADERBOARD);
        } else if (view.getId() == R.id.sign_in_button) {
            // start the asynchronous sign in flow
            beginUserInitiatedSignIn();

        }
        else if (view.getId() == R.id.sign_out_button) {
            // sign out.
            signOut();

            // show sign-in button, hide the sign-out button
            findViewById(R.id.sign_in_button).setVisibility(View.VISIBLE);
            findViewById(R.id.sign_out_button).setVisibility(View.GONE);
        }


    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Games.Leaderboards.submitScore(getApiClient(), getString(R.string.the_best_players), resultCode);
        if(requestCode==1) {
            if(resultCode > leseHighscore()) {
                schreibeHighscore(resultCode);
            }
        }
    }

    @Override
    public void onSignInFailed() {

        findViewById(R.id.sign_in_button).setVisibility(View.VISIBLE);
        findViewById(R.id.sign_out_button).setVisibility(View.GONE);


    }

    @Override
    public void onSignInSucceeded() {



            View a = findViewById(R.id.highscorebutton);
            a.setVisibility(View.VISIBLE);

            View b = findViewById(R.id.button3);
            b.setVisibility(View.VISIBLE);

            findViewById(R.id.sign_in_button).setVisibility(View.GONE);
            findViewById(R.id.sign_out_button).setVisibility(View.VISIBLE);




    }

}

Thanks, GoogleWelt


回答1:


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");



回答2:


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


来源:https://stackoverflow.com/questions/24474986/android-google-play-games-services-connection-error-java-lang-illegalstateexc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!