简体   繁体   中英

LibGDX/Google Play Services Error

I have been trying to implement Google Play Services into my LibGDX game, and have been using the real-time Multiplayer APK.

However, after everyone has joined the room through auto-matching, I try to start the game through calling a method to change the screen, but i get the error as followed. Even if i removed the contents of the method, the same error still occurs. Could anyone enlighten me?

Thank you!

The error logged in the console is ,

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=10002, result=-1, data=Intent { (has extras) }} to activity {com.mygdx.game/com.mygdx.game.AndroidLauncher}: java.lang.NullPointerException

Caused by: java.lang.NullPointerException at com.mygdx.game.GSGameHelper.onActivityResult(GSGameHelper.java:76) --> which points to this.game.multiplayerready()

Code as Follows:

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

This is where i initiate the class

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

And The class constructor is as follows

public MacroHardv2(ActionResolver actionResolver) {
    this.actionResolver = actionResolver;
    actionResolver.setGame(this);
}

You are trying to call

this.game.multiplayerGameReady();

But you probably didn't set "this.game" anywhere. Where did you define your "game" object. Can you please show the code block that you define it and also set it, or instantiate it.

So "this.game" is your null object you trying to use.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM