4

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

2 回答 2

4

如果您正在使用BaseGameActivity,请不要调用GamesClient.connect(). 使用的好处BaseGameActivity是它为您处理所有连接样板。您所要做的就是覆盖onSignInSucceeded并从那里进行 API 调用。在获得onSignInSucceeded.

另外,请记住,当您的 Activity 获得 时onStop,游戏 API 将断开连接。之后,当它随后得到一个 时onStart,您将再次等待,onSignInSucceeded然后再进行任何 API 调用。

于 2013-06-12T19:59:26.243 回答
3

您正在尝试在建立连接之前提交分数。

就像错误所说的那样,您应该等待 onConnected() ,然后才应该允许提交分数。示例:您应该只在连接后显示按钮

如果您正在扩展 BaseGameActivity,您应该执行以下操作:

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

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

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

@Override
public void onSignInFailed() {
    super.onSignInFailed();
    // do not allow to submit scores
}
于 2013-06-12T12:08:46.053 回答