安卓:谷歌玩游戏服务连接错误(java.lang.IllegalStateException:GoogleApiClient必须连接)

问题描述:

我已经编程的游戏为Android,一切工作正常,但现在我想我的应用程序有谷歌玩游戏服务(排行榜和achivments)。我用谷歌的例子code登录到谷歌服务(在脚本没有错误),但每次我想用我的应用程序在连接调试茂德,我得到这个错误:

i've programmed a game for android, everything works fine, but now i want my app to have Google play Games services (leaderboards and achivments). I used the Google example code to log in to the Google services (no errors in the script), but everytime i want to connect with my App in debug maode, 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)

和应用程序崩溃。这是我的$ C $下在那里我希望它连接MainActivity:

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




    }

}

谢谢,GoogleWelt

Thanks, GoogleWelt

按照官方文档,在执行任何操作,GoogleApiClient必须连接

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

当在未连接用户(签署),并点击显示的排行榜或者成就,它导致抛出的异常。修改您的code发动排行榜是这样的:

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