我一直在尝试将Google Play服务应用到我的LibGDX游戏中,并且一直在使用实时多人游戏APK。
然而,在每个人都通过自动匹配加入房间后,我尝试通过调用方法来更改屏幕来启动游戏,但是我得到了如下错误。即使我删除了方法的内容,仍然会发生相同的错误。谁能开导我?
谢谢!
控制台中记录的错误是
java.lang.RuntimeException:传递结果失败 ResultInfo {who = null,request = 10002,result = -1,data = Intent {(has 额外的)}}到活动 {} com.mygdx.game/com.mygdx.game.AndroidLauncher: 显示java.lang.NullPointerException
引起:java.lang.NullPointerException 在com.mygdx.game.GSGameHelper.onActivityResult(GSGameHelper.java:76) - > 它指向this.game.multiplayerready()
代码如下:
public void onActivityResult(int request,int response, Intent data){
if (request == GSGameHelper.RC_WAITING_ROOM){
if (response == Activity.RESULT_CANCELED || response == GamesActivityResultCodes.RESULT_LEFT_ROOM ){
Games.RealTimeMultiplayer.leave(getApiClient(), this, mRoomID);
activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
BaseGameUtils.showAlert(activity, "Left Room");
}else{
BaseGameUtils.showAlert(activity, "Game Starting!");
this.game.multiplayerGameReady();
}
}
else if (request == GSGameHelper.RC_SELECT_PLAYERS){
if (response != Activity.RESULT_OK) {
// user canceled
return;
}
// get the invitee list
Bundle extras = data.getExtras();
final ArrayList<String> invitees =
data.getStringArrayListExtra(Games.EXTRA_PLAYER_IDS);
// get auto-match criteria
Bundle autoMatchCriteria = null;
int minAutoMatchPlayers =
data.getIntExtra(Multiplayer.EXTRA_MIN_AUTOMATCH_PLAYERS, 0);
int maxAutoMatchPlayers =
data.getIntExtra(Multiplayer.EXTRA_MAX_AUTOMATCH_PLAYERS, 0);
Gdx.app.log("J", "Jmin" + minAutoMatchPlayers + " Jmax:" + maxAutoMatchPlayers);
for (String invitee : invitees){
Gdx.app.log("L" , invitee);
}
if (minAutoMatchPlayers > 0) {
autoMatchCriteria = RoomConfig.createAutoMatchCriteria(
minAutoMatchPlayers, maxAutoMatchPlayers, 0);
} else {
autoMatchCriteria = null;
}
// create the room and specify a variant if appropriate
RoomConfig.Builder roomConfigBuilder = makeBasicRoomConfigBuilder();
roomConfigBuilder.addPlayersToInvite(invitees);
if (autoMatchCriteria != null) {
roomConfigBuilder.setAutoMatchCriteria(autoMatchCriteria);
}
RoomConfig roomConfig = roomConfigBuilder.build();
Games.RealTimeMultiplayer.create(getApiClient(), roomConfig);
// prevent screen from sleeping during handshake
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}else{
super.onActivityResult(request, response, data);
}
}
public class MacroHardv2 extends ApplicationAdapter {
public void multiplayerGameReady(){
//gamew.multiplayer = true;
//Gdx.app.log("EMPEZANDO", "Starting Game");
//gsm.set(new PlayState(gsm));
//dispose();
}
}
这是我发起课程的地方
public class AndroidLauncher extends AndroidApplication implements ActionResolver{
private GSGameHelper _gameHelper;
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
_gameHelper = new GSGameHelper(this, GameHelper.CLIENT_GAMES);
_gameHelper.enableDebugLog(false);
GameHelperListener gameHelperListerner = new GameHelper.GameHelperListener() {
@Override
public void onSignInSucceeded() {
// TODO Auto-generated method stub
}
@Override
public void onSignInFailed() {
// TODO Auto-generated method stub
}
};
_gameHelper.setup(gameHelperListerner);
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();config.useImmersiveMode = true;
initialize(new MacroHardv2(this), config);
}
并且类构造函数如下
public MacroHardv2(ActionResolver actionResolver) {
this.actionResolver = actionResolver;
actionResolver.setGame(this);
}
答案 0 :(得分:1)
您正在尝试致电
this.game.multiplayerGameReady();
但你可能没有在任何地方设置“this.game”。你在哪里定义了你的“游戏”对象。你能否展示你定义它的代码块,并设置它,或者实例化它。
所以“this.game”是您尝试使用的空对象。