LibGDX和GoogleApiClient设置/集成(第一次) [英] LibGDX and the GoogleApiClient setup/integration (first time)

查看:107
本文介绍了LibGDX和GoogleApiClient设置/集成(第一次)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直想尝试添加Google Play,然后在Play商店中发布我的应用.我是通过实现Screen类在LibGDX中实现的,现在突然Google指南试图让我创建layout.xml并将大量新内容添加到我的应用中,只是为了获得一个愚蠢的登录"按钮. /p>

关于LibGDX项目,几乎不可能遵循此主题的Google文档,而且LibGDX社区论坛上没有太多帮助.

这不是那么困难,我显然错过了一些简单的事情!我相信我已经将GoogleApiClient和自己的界面设置为OK,并且在我现在需要的所有类中都有一个GoogleApiClient实例,而没有错误.

当我尝试制作愚蠢的布局并将其放置在我的应用程序的开头时出现错误(IE.我试图创建一个简单的layout.xml,仅当玩家单击登录后才具有登录"按钮-在应用程序中,将照常在LibGDX And​​roid启动器中调用初始化...".

当前我收到此错误:

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) 

我要在Pastebin上发布引起问题的整个代码,并将链接放在此处:

http://pastebin.com/F6sqQPDc

解决方案

希望您使用的是Intellij或Android Studio.

如果是,则将BaseGameUtils集成到您的项目中

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

并将ActionResolver保留在核心模块中以进行接口.

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

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

解决方案

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

这篇关于LibGDX和GoogleApiClient设置/集成(第一次)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆