本文介绍了GamesClient没有连接。错误:"调用connect(),并等待onConnected()被称为"。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 GamesClient 使用的谷歌玩游戏服务排行榜。现在我有这么当 importbutton 被点击, GamesClient 用于提交了一些分数。如下图所示,我得到一个错误说连接(),并等待onConnectd()被调用。

我是什么做错了吗?我看到一些教程一些所谓的 PlusClient 。我需要在某种方式?如果需要,我可以提供更多的code。

看来,很多问题都在计算器这个新的谷歌玩游戏服务,但​​没有很多答案在那里。貌似人还在学习 - 像​​我一样。 :)

LogCat中

  06-12 00:40:40.173:E / AndroidRuntime(1685):java.lang.IllegalStateException:未连接。调用connect(),并等待onConnected()被调用。
06-12 00:40:40.173:E / AndroidRuntime(1685):在com.google.android.gms.internal.pn(来源不明)
06-12 00:40:40.173:E / AndroidRuntime(1685):在com.google.android.gms.internal.po(来源不明)
06-12 00:40:40.173:E / AndroidRuntime(1685):在com.google.android.gms.internal.bj.a(来源不明)
06-12 00:40:40.173:E / AndroidRuntime(1685):在com.google.android.gms.games.GamesClient.submitScore(来源不明)
06-12 00:40:40.173:E / AndroidRuntime(1685):在matt.lyons.bibletrivia.lite.MainMenu $ 8.onClick(MainMenu.java:173)
06-12 00:40:40.173:E / AndroidRuntime(1685):在android.view.View.performClick(View.java:4204)
06-12 00:40:40.173:E / AndroidRuntime(1685):在android.view.View $ PerformClick.run(View.java:17355)
06-12 00:40:40.173:E / AndroidRuntime(1685):在android.os.Handler.handleCallback(Handler.java:725)
06-12 00:40:40.173:E / AndroidRuntime(1685):在android.os.Handler.dispatchMessage(Handler.java:92)
06-12 00:40:40.173:E / AndroidRuntime(1685):在android.os.Looper.loop(Looper.java:137)
06-12 00:40:40.173:E / AndroidRuntime(1685):在android.app.ActivityThread.main(ActivityThread.java:5041)
06-12 00:40:40.173:E / AndroidRuntime(1685):在java.lang.reflect.Method.invokeNative(本机方法)
06-12 00:40:40.173:E / AndroidRuntime(1685):在java.lang.reflect.Method.invoke(Method.java:511)
06-12 00:40:40.173:E / AndroidRuntime(1685):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793)
06-12 00:40:40.173:E / AndroidRuntime(1685):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-12 00:40:40.173:E / AndroidRuntime(1685):在dalvik.system.NativeStart.main(本机方法)
 

MainMenu.java

 公共类MainMenu的扩展BaseGameActivity {

    DatabaseHelper DH;
    GamesClient客户端;
    语境℃;

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.mainmenu);

        客户= getGamesClient();
        client.connect();

        C =这一点;

        DH =新DatabaseHelper(本);
        dh.openDB();

        importbutton =(按钮)findViewById(R.id.importbutton);

        importbutton.setOnClickListener(新OnClickListener(){
            公共无效的onClick(视图v){
                importScores();
            }
        });
    }

    公共无效importScores(){

        最后一个对话框对话框=新的对话框(C);
        dialog.setContentView(R.layout.importlayout);
        dialog.setTitle(R.string.importtitle);

        TextView的问题=(TextView中)dialog.findViewById(R.id.question);
        按钮保存=(按钮)dialog.findViewById(R.id.save);
        按钮从无到有=(按钮)dialog.findViewById(R.id.scratch);

        question.setText(c.getResources()的getString(R.string.importquestion));
        save.setText(c.getResources()的getString(R.string.savebtn));
        scratch.setText(c.getResources()的getString(R.string.scratchbtn));

        save.setOnClickListener(新OnClickListener(){
            公共无效的onClick(视图v){

                长highestJC = dh.getHighestJC();
                client.submitScore(c.getResources()的getString(R.string.leaderboardjc),highestJC);

                长highestTenC = dh.getHighestTenC();
                client.submitScore(c.getResources()的getString(R.string.leaderboardtenc),highestTenC);

                长highestExodus = dh.getHighestExodus();
                client.submitScore(c.getResources()的getString(R.string.leaderboardexodus),highestExodus);

                长highestGenesis = dh.getHighestGenesis();
                client.submitScore(c.getResources()的getString(R.string.leaderboardgenesis),highestGenesis);

                长highestHolydays = dh.getHighestHolydays();
                client.submitScore(c.getResources()的getString(R.string.leaderboardholydays),highestHolydays);

                长highestFacts = dh.getHighestFacts();
                client.submitScore(c.getResources()的getString(R.string.leaderboardfacts),highestFacts。);

                长highestActs = dh.getHighestActs();
                client.submitScore(c.getResources()的getString(R.string.leaderboardacts),highestActs。);

                长highestRandom = dh.getHighestRandom();
                client.submitScore(c.getResources()的getString(R.string.leaderboardrandom),highestRandom);

                长highestAll = dh.getHighestAll();
                client.submitScore(c.getResources()的getString(R.string.leaderboardallcats),highestAll);

                dialog.dismiss();
            }
        });

        scratch.setOnClickListener(新OnClickListener(){
            公共无效的onClick(视图v){
                dh.deleteAll();
                的for(int i = 0; I< 15;我++){
                    dh.insert(0,0, - );
                }
                dialog.dismiss();
                dh.closeDB();
            }
        });

        dialog.show();
    }
}
 

解决方案

您试图提交分数建立连接之前。

像错误说,你应该等待onConnected(),只有那么你应该让分数提交。例:你应该只显示按钮,它连接后,

如果要扩展BaseGameActivity,你应该做的是这样的:

  INT isGooglePlayServiceAvilable = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
如果(isGooglePlayServiceAvilable == ConnectionResult.SUCCESS){
    beginUserInitiatedSignIn();
} 其他 {
    GooglePlayServicesUtil.getErrorDialog(isGooglePlayServiceAvilable,MainMenu.this,REQUEST_DIALOG).show();
}
 

然后,你应该重写这些方法:

  @覆盖
公共无效onSignInSucceeded(){
   super.onSignInSucceeded();
   //允许提交分数
}

@覆盖
公共无效onSignInFailed(){
    super.onSignInFailed();
    //不允许提交分数
}
 

I am trying to use GamesClient to use the leaderboards of the Google Play Game Services. Right now I have it so when the importbutton is clicked, the GamesClient is used to submit some scores. As seen below I get an error saying connect() and wait for onConnectd() to be called.

What am I doing wrong? I see in some tutorials something called a PlusClient. Do I need that in some way? I can provide more code if needed.

It seems that a lot of questions are on StackOverflow about this new Google Play Game Services but there is not a lot of answers out there. Looks like people are still learning -- like myself. :)

LogCat

06-12 00:40:40.173: E/AndroidRuntime(1685): java.lang.IllegalStateException: Not connected. Call connect() and wait for onConnected() to be called.
06-12 00:40:40.173: E/AndroidRuntime(1685):     at com.google.android.gms.internal.p.n(Unknown Source)
06-12 00:40:40.173: E/AndroidRuntime(1685):     at com.google.android.gms.internal.p.o(Unknown Source)
06-12 00:40:40.173: E/AndroidRuntime(1685):     at com.google.android.gms.internal.bj.a(Unknown Source)
06-12 00:40:40.173: E/AndroidRuntime(1685):     at com.google.android.gms.games.GamesClient.submitScore(Unknown Source)
06-12 00:40:40.173: E/AndroidRuntime(1685):     at matt.lyons.bibletrivia.lite.MainMenu$8.onClick(MainMenu.java:173)
06-12 00:40:40.173: E/AndroidRuntime(1685):     at android.view.View.performClick(View.java:4204)
06-12 00:40:40.173: E/AndroidRuntime(1685):     at android.view.View$PerformClick.run(View.java:17355)
06-12 00:40:40.173: E/AndroidRuntime(1685):     at android.os.Handler.handleCallback(Handler.java:725)
06-12 00:40:40.173: E/AndroidRuntime(1685):     at android.os.Handler.dispatchMessage(Handler.java:92)
06-12 00:40:40.173: E/AndroidRuntime(1685):     at android.os.Looper.loop(Looper.java:137)
06-12 00:40:40.173: E/AndroidRuntime(1685):     at android.app.ActivityThread.main(ActivityThread.java:5041)
06-12 00:40:40.173: E/AndroidRuntime(1685):     at java.lang.reflect.Method.invokeNative(Native Method)
06-12 00:40:40.173: E/AndroidRuntime(1685):     at java.lang.reflect.Method.invoke(Method.java:511)
06-12 00:40:40.173: E/AndroidRuntime(1685):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-12 00:40:40.173: E/AndroidRuntime(1685):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-12 00:40:40.173: E/AndroidRuntime(1685):     at dalvik.system.NativeStart.main(Native Method)

MainMenu.java

public class MainMenu extends BaseGameActivity {

    DatabaseHelper dh;
    GamesClient client;
    Context c;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mainmenu);

        client = getGamesClient();
        client.connect();

        c = this;

        dh = new DatabaseHelper(this);
        dh.openDB();

        importbutton = (Button)findViewById(R.id.importbutton);

        importbutton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                importScores();
            }
        });
    }

    public void importScores() {

        final Dialog dialog = new Dialog(c);
        dialog.setContentView(R.layout.importlayout);
        dialog.setTitle(R.string.importtitle);

        TextView question = (TextView)dialog.findViewById(R.id.question);
        Button save = (Button)dialog.findViewById(R.id.save);
        Button scratch = (Button)dialog.findViewById(R.id.scratch);

        question.setText(c.getResources().getString(R.string.importquestion));
        save.setText(c.getResources().getString(R.string.savebtn));
        scratch.setText(c.getResources().getString(R.string.scratchbtn));

        save.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {

                long highestJC = dh.getHighestJC();
                client.submitScore(c.getResources().getString(R.string.leaderboardjc), highestJC);

                long highestTenC = dh.getHighestTenC();
                client.submitScore(c.getResources().getString(R.string.leaderboardtenc), highestTenC);

                long highestExodus = dh.getHighestExodus();
                client.submitScore(c.getResources().getString(R.string.leaderboardexodus), highestExodus);

                long highestGenesis = dh.getHighestGenesis();
                client.submitScore(c.getResources().getString(R.string.leaderboardgenesis), highestGenesis);

                long highestHolydays = dh.getHighestHolydays();
                client.submitScore(c.getResources().getString(R.string.leaderboardholydays), highestHolydays);

                long highestFacts = dh.getHighestFacts();
                client.submitScore(c.getResources().getString(R.string.leaderboardfacts), highestFacts);

                long highestActs = dh.getHighestActs();
                client.submitScore(c.getResources().getString(R.string.leaderboardacts), highestActs);

                long highestRandom = dh.getHighestRandom();
                client.submitScore(c.getResources().getString(R.string.leaderboardrandom), highestRandom);

                long highestAll = dh.getHighestAll();
                client.submitScore(c.getResources().getString(R.string.leaderboardallcats), highestAll);

                dialog.dismiss();
            }
        });

        scratch.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                dh.deleteAll();
                for(int i = 0; i < 15; i++) {
                    dh.insert(0, 0, "-");
                }
                dialog.dismiss();
                dh.closeDB();
            }
        });

        dialog.show();
    }
}
解决方案

You are trying to submit the scores before a connection is established.

Like the error says, you should wait for onConnected() and only then you should allow the score submission. Example: you should only show the button after it is connected

If you are extending BaseGameActivity, you should do something like this:

int isGooglePlayServiceAvilable = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
if (isGooglePlayServiceAvilable == ConnectionResult.SUCCESS) {
    beginUserInitiatedSignIn();
} else {
    GooglePlayServicesUtil.getErrorDialog(isGooglePlayServiceAvilable, MainMenu.this, REQUEST_DIALOG).show();
}

And then, you should override these methods:

@Override
public void onSignInSucceeded() {
   super.onSignInSucceeded();
   // allow to submit scores
}

@Override
public void onSignInFailed() {
    super.onSignInFailed();
    // do not allow to submit scores
}

这篇关于GamesClient没有连接。错误:&QUOT;调用connect(),并等待onConnected()被称为&QUOT;。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 14:07
查看更多