Android:Google Play游戏服务连接错误(java.lang.IllegalStateException:必须连接GoogleApiClient。)

问题描述 投票:8回答:2

我为Android设计了一款游戏,一切正常,但现在我希望我的应用程序能够让Google玩游戏服务(排行榜和成就)。我使用Google示例代码登录Google服务(脚本中没有错误),但每次我想在调试模式下与我的应用程序连接时,都会收到以下错误:

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)

而App崩溃了。这是我希望它连接的MainActivity的代码:

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

java android google-play-services
2个回答
3
投票

根据official documentation,“在执行任何操作之前,必须连接GoogleApiClient”

当用户未连接(登录)并单击以显示排行榜或成就时,会导致抛出异常。修改您的代码以启动排行榜,如下所示:

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

}

使用相同的逻辑来显示成就:

 if (isSignedIn()) 
 startActivityForResult(Games.Achievements.getAchievementsIntent(getApiClient()), REQUEST_ACHIEVEMENT);
 else showAlert("Please sign in to view achievements");

0
投票

检查你获得ApiClient的部分,即getApiClient()。编写下面的代码,看看GoogleApiClient是否已连接。

   GoogleApiClient mGoogleApiClient;
    if(mGoogleApiClient.isConnected()){
     // good
    }else{
     //connect it
     mGoogleApiClient.connect(GoogleApiClient.SIGN_IN_MODE_OPTIONAL);
    }
© www.soinside.com 2019 - 2024. All rights reserved.