0

我是新手,所以请原谅我的不良编码习惯,但我遇到了一个IllegalStateException错误,但我不知道为什么。

我正在制作一个游戏,我希望每个玩家的比赛是 1 回合。所以,如果你和我一起玩,我开始比赛,那么我会先轮到我,然后给你发送所需的信息,然后你轮到你玩,然后把你的数据和比赛结果一起发回。

但是,我在完成轮到我时遇到问题(“我的”是开始比赛的人)。我想将信息发送给其他玩家,但是(根据我收集的信息)我必须完成比赛并发送所需的数据。我使用以下代码执行此操作:

String playerId = Games.Players.getCurrentPlayerId(mGoogleApiClient);
                        String myParticipantId = mMatch.getParticipantId(playerId);
                        ParticipantResult myresult = new ParticipantResult(myParticipantId,ParticipantResult.MATCH_RESULT_UNINITIALIZED, ParticipantResult.PLACING_UNINITIALIZED);
                        ParticipantResult opponentresult = new ParticipantResult(getNextParticipantId(),ParticipantResult.MATCH_RESULT_UNINITIALIZED, ParticipantResult.PLACING_UNINITIALIZED);
                        List<ParticipantResult> reportresult = new ArrayList<ParticipantResult>();
                        reportresult.add(myresult);
                        reportresult.add(opponentresult);
                        Games.TurnBasedMultiplayer.finishMatch(mGoogleApiClient, mMatch.getMatchId(),mTurnData.persist(), reportresult)
                        .setResultCallback(new ResultCallback<TurnBasedMultiplayer.UpdateMatchResult>() {
                            @Override
                            public void onResult(TurnBasedMultiplayer.UpdateMatchResult result) {
                                processResult(result);
                            }
                        });

我得到的错误在我的帖子底部提到。第 2717 行是这一行:

ParticipantResult myresult = new ParticipantResult(myParticipantId,ParticipantResult.MATCH_RESULT_UNINITIALIZED, ParticipantResult.PLACING_UNINITIALIZED);

那么我在这里做错了什么?据我所知,这应该可行...老实说,我想避免发送参与者结果,但是由于我需要发送数据,因此我需要根据以下内容提及结果:com.google.android.gms.games.multiplayer.turnbased.TurnBasedMultiplayer.finishMatch(GoogleApiClient arg0, String arg1, byte[] arg2, List<ParticipantResult> arg3)

12-25 04:41:36.995: E/AndroidRuntime(19132): FATAL EXCEPTION: main
12-25 04:41:36.995: E/AndroidRuntime(19132): Process: com.devsid.quiz, PID: 19132
12-25 04:41:36.995: E/AndroidRuntime(19132): java.lang.IllegalStateException
12-25 04:41:36.995: E/AndroidRuntime(19132):    at com.google.android.gms.internal.jx.K(Unknown Source)
12-25 04:41:36.995: E/AndroidRuntime(19132):    at com.google.android.gms.games.multiplayer.ParticipantResult.<init>(Unknown Source)
12-25 04:41:36.995: E/AndroidRuntime(19132):    at com.google.android.gms.games.multiplayer.ParticipantResult.<init>(Unknown Source)
12-25 04:41:36.995: E/AndroidRuntime(19132):    at com.devsid.quiz.QuizActivity$52.onClick(QuizActivity.java:2717)
12-25 04:41:36.995: E/AndroidRuntime(19132):    at android.view.View.performClick(View.java:4438)
12-25 04:41:36.995: E/AndroidRuntime(19132):    at android.view.View$PerformClick.run(View.java:18439)
12-25 04:41:36.995: E/AndroidRuntime(19132):    at android.os.Handler.handleCallback(Handler.java:733)
12-25 04:41:36.995: E/AndroidRuntime(19132):    at android.os.Handler.dispatchMessage(Handler.java:95)
12-25 04:41:36.995: E/AndroidRuntime(19132):    at android.os.Looper.loop(Looper.java:136)
12-25 04:41:36.995: E/AndroidRuntime(19132):    at android.app.ActivityThread.main(ActivityThread.java:5158)
12-25 04:41:36.995: E/AndroidRuntime(19132):    at java.lang.reflect.Method.invokeNative(Native Method)
12-25 04:41:36.995: E/AndroidRuntime(19132):    at java.lang.reflect.Method.invoke(Method.java:515)
12-25 04:41:36.995: E/AndroidRuntime(19132):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
12-25 04:41:36.995: E/AndroidRuntime(19132):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
12-25 04:41:36.995: E/AndroidRuntime(19132):    at dalvik.system.NativeStart.main(Native Method)    
4

1 回答 1

1

I found that you cannot really hold match data in a static and still call it using the google functions, maybe it is coded this way to stop too much traffic.

You can store the matchID into a variable and use it to make sure that you are updating for the correct match, but you cannot call google methods of match, or you will get NULL results.

From my experience, the only time you can call the google match methods is inside your processResult() function. Like this...

public void processResult(UpdateMatchResult result) {
    TurnBasedMatch updateMatch = result.getMatch();

Otherwise the data is stale.

于 2015-12-08T11:35:42.793 回答