Android Games.RealtimeMultiplayer.getWaitingRoomIntent空指针异常
我创建了一个房间并成功地创建了它。我的Android Games.RealtimeMultiplayer.getWaitingRoomIntent空指针异常,android,nullpointerexception,google-play-services,google-play-games,Android,Nullpointerexception,Google Play Services,Google Play Games,我创建了一个房间并成功地创建了它。我的onRoomCreated方法被调用 @Override public void onRoomCreated(int statusCode, Room room) { mRoomId = room.getRoomId(); Intent i = Games.RealTimeMultiplayer.getWaitingRoomIntent(gApiClient, room, 2); startActivi
onRoomCreated
方法被调用
@Override
public void onRoomCreated(int statusCode, Room room) {
mRoomId = room.getRoomId();
Intent i = Games.RealTimeMultiplayer.getWaitingRoomIntent(gApiClient, room, 2);
startActivityForResult(i, RC_WAITING_ROOM);
}
然后在我的onActivityResult
中
Room r = data.getExtras().getParcelable(Multiplayer.EXTRA_ROOM);
ArrayList<String> invitees = new ArrayList<String>();
for (Participant p : r.getParticipants()) {
invitees.add(p.getPlayer().getPlayerId()); //<---NULL POINTER!
}
Room r=data.getExtras().getParcelable(多人游戏.额外房间);
ArrayList invites=新的ArrayList();
对于(参与者p:r.getParticipants()){
被邀请者.add(p.getPlayer().getPlayerId());//现在我更清楚地看到了你的要求(是我的错,不是你的错),下面是我的做法:
(为了澄清,我使用了LibGDX,所以可能有些接口是您不需要的,我仍然在使用GamesClient,不是新的API方法,但出于所有目的都是一样的)
首先,我希望开始比赛的最后一个电话是接通的
@Override
public void onRoomConnected(int statusCode, Room room) {
//dLog("onRoomConnected");
mRoomCurrent = room;
mParticipants = room.getParticipants();
mMyID = room.getParticipantId(aHelper.getGamesClient().getCurrentPlayerId());
//dLog("The id is " + mMyID);
try {
bWaitRoomDismissedFromCode = true;
finishActivity(RC_WAITING_ROOM);
} catch (Exception e) {
//dLog("would have errored out in waiting room");
}
//tell the Game the room is connected
if (statusCode == GamesClient.STATUS_OK) {
theGameInterface.onRoomConnected(room.getParticipantIds(), mMyID, room.getCreationTimestamp() );
} else {
leaveRoom();
}
}
所以,现在有了所有的参与者。现在在我的游戏代码(我发送ID列表的地方)中,我对ID列表进行排序,以便在确定玩家顺序时,对所有玩家都采用相同的方法。首先,我建立我的对手
private void buildOpponents() {
// this creates a new opponent with a View on the Stage()
//sort the participants the same for all players
sortParticipantIDs();
for (String s : mParticipantIds) {
if(s.contains(mMyID) || mMyID.contains(s)) continue;
newOpponentWindow ow = new newOpponentWindow(s, MyAssetManager.getMySkin(), getStage());
Opponent o = new Opponent(this, s);
mapOpponents.put(s, o);
o.setWindow(ow);
getStage().addActor(ow);
}
setOpponentWindowPositions();
}
然后,在进行了更多的设置之后,我开始比赛,我第一次通过了,我选择了谁是顶级ID,谁就有资格开始比赛(我发现这已经足够随机化了,不需要做其他的方法……但是你可以让顶级ID做其他的方法,并发送给其他玩家)请注意,如果有人在游戏后期离开房间,这会检查我的对手以确定首发球员
private boolean determineIfStartingBidder() {
Collections.sort(mParticipantIds);
// now look thru list
// if the number is mine then return true
// if the number is not mine.. and opponent is not Out of Game or Disconnected.. then return false
for (String s : mParticipantIds) {
if(s.contains(mMyID) || mMyID.contains(s)){
return true;
}
if(mapOpponents.get(s).getCurrentState() == currentState.DISCONNECTED || mapOpponents.get(s).getCurrentState() == currentState.OUTOFGAME ||
mapOpponents.get(s).getCurrentState() == currentState.LOSTGAME) {
continue;
}
return false;
}
return false;
}
然后在你的游戏逻辑中,只要以任何有意义的方式浏览你的ParticipantID列表就可以了!这很有效,因为所有传递消息的调用都需要ParticipantID,并且都是为了方便抓取和使用
以下是先前的答复------------------------------------------------
试一试
不需要getExtrasD'oh!误读了,以为您在请求房间时得到了空值!:)
data.getParcelableExtra(Multiplayer.EXTRA_ROOM);