Search code examples
javalibgdxgoogle-apigoogle-play-servicesgoogle-api-client

LibGDX and the GoogleApiClient setup/integration (first time)


I'm stuck trying to add Google Play and then release my app on the Play Store. I made it in LibGDX by implementing the Screen class and now all of a sudden the Google guides are trying to make me create layout.xml and add loads of new stuff into my app simply to get a silly "sign-in" button.

The Google documentation on this subject is almost impossible to follow with a LibGDX project, and there is not much help on the LibGDX community forum.

This cannot be that difficult, I have obviously missed something simple! I believe that I have the GoogleApiClient and my own interface set up OK, and I have an instance of GoogleApiClient inside all of my classes that need it now, without errors.

The error comes when I try to make the stupid layout and have this at the beginning of my app (IE. I tried to create a simple layout.xml that just has the Sign-In button only, once the player clicks sign-in the app would start by calling "initialize..." inside the LibGDX Android Launcher as usual.

Currently I get this error:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.megabro.flappyturd, PID: 3799
                  java.lang.RuntimeException: Unable to resume activity {com.megabro.flappyturd/com.megabro.flappyturd.AndroidLauncher}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.badlogic.gdx.backends.android.AndroidInput.onResume()' on a null object reference
                      at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3400)
                      at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3440)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2713)
                      at android.app.ActivityThread.-wrap12(ActivityThread.java)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:154)
                      at android.app.ActivityThread.main(ActivityThread.java:6077)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
                   Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.badlogic.gdx.backends.android.AndroidInput.onResume()' on a null object reference
                      at com.badlogic.gdx.backends.android.AndroidApplication.onResume(AndroidApplication.java:299)
                      at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1269)
                      at android.app.Activity.performResume(Activity.java:6766)
                      at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3377)
                      at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3440) 
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2713) 
                      at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:154) 
                      at android.app.ActivityThread.main(ActivityThread.java:6077) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 

I'm posting the whole code that is causing issue on Pastebin, will put link here:

http://pastebin.com/F6sqQPDc


Solution

  • Hopefully you are using Intellij or Android Studio.

    If yes integrate BaseGameUtils in your project

    https://github.com/libgdx/libgdx/wiki/Google-Play-Game-Services-in-LibGDX#intellij-and-android-studio-setup

    public class MainActivity extends AndroidApplication implements GameHelperListener, ActionResolver {
        private GameHelper gameHelper;
    
        @Override
        public void onCreate (Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            initialize(new TutorialLibgdxGameservices(this), false);
        if (gameHelper == null) {
          gameHelper = new GameHelper(this, GameHelper.CLIENT_GAMES);
          gameHelper.enableDebugLog(true);
        }
        gameHelper.setup(this);
        }
    
        @Override
        public void onStart(){
            super.onStart();
            gameHelper.onStart(this);
        }
    
        @Override
        public void onStop(){
            super.onStop();
            gameHelper.onStop();
        }
    
        @Override
        public void onActivityResult(int request, int response, Intent data) {
            super.onActivityResult(request, response, data);
            gameHelper.onActivityResult(request, response, data);
        }
    
        @Override
        public boolean getSignedInGPGS() {
            return gameHelper.isSignedIn();
        }
    
        @Override
        public void loginGPGS() {
            try {
                runOnUiThread(new Runnable(){
                    public void run() {
                        gameHelper.beginUserInitiatedSignIn();
                    }
                });
            } catch (final Exception ex) {
            }
        }
    
        @Override
        public void submitScoreGPGS(int score) {
            Games.Leaderboards.submitScore(gameHelper.getApiClient(), "CgkI6574wJUXEAIQBw", score);
        }
    
        @Override
        public void unlockAchievementGPGS(String achievementId) {
          Games.Achievements.unlock(gameHelper.getApiClient(), achievementId);
        }
    
        @Override
        public void getLeaderboardGPGS() {
          if (gameHelper.isSignedIn()) {
            startActivityForResult(Games.Leaderboards.getLeaderboardIntent(gameHelper.getApiClient(), "CgkI6574wJUXEAIQBw"), 100);
          }
          else if (!gameHelper.isConnecting()) {
            loginGPGS();
          }
        }
    
        @Override
        public void getAchievementsGPGS() {
          if (gameHelper.isSignedIn()) {
            startActivityForResult(Games.Achievements.getAchievementsIntent(gameHelper.getApiClient()), 101);
          }
          else if (!gameHelper.isConnecting()) {
            loginGPGS();
          }
        }
    
        @Override
        public void onSignInFailed() {
        }
    
        @Override
        public void onSignInSucceeded() {
        }
    }
    

    And keep ActionResolver in core module for interfacing.

    public interface ActionResolver {
        public boolean getSignedInGPGS();
        public void loginGPGS();
        public void submitScoreGPGS(int score);
        public void unlockAchievementGPGS(String achievementId);
        public void getLeaderboardGPGS();
        public void getAchievementsGPGS();
    }