因此,我想说我正在使用LibGDX会有所帮助。

代码:

package com.fmeg.tapout.android;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.applifier.impact.android.ApplifierImpact;
import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
import com.fmeg.tapout.ActionResolver;
import com.fmeg.tapout.TapoutGame;
import com.google.android.gms.games.Games;
import com.google.example.games.basegameutils.GameHelper;
import com.google.example.games.basegameutils.GameHelper.GameHelperListener;

public class AndroidLauncher extends AndroidApplication
    implements GameHelperListener, ActionResolver {

    private static final String EVERPLAY_GAME_ID = "14234";

    private GameHelper gameHelper;

    public AndroidLauncher() {

    }

    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();

        ApplifierImpact.setDebugMode(false);
        ApplifierImpact.setTestMode(false);
        ApplifierImpact impact = new ApplifierImpact((Activity)this, EVERPLAY_GAME_ID);
        ApplifierImpact.instance.changeActivity(this);

        gameHelper = new GameHelper(this, GameHelper.CLIENT_ALL);
        gameHelper.setup(this);

        initialize(new TapoutGame(impact, this), config);

    }

    @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 void onSignInFailed() {
        // TODO Auto-generated method stub

    }

    @Override
    public void onSignInSucceeded() {
        // TODO Auto-generated method stub

    }

    @Override
    public void unlockAchievement(String achievementID) {
        Games.Achievements.unlock(gameHelper.getApiClient(), achievementID);
    }

    @Override
    public void achivementProgress(String achievementID, int progression) {
        Games.Achievements.increment(gameHelper.getApiClient(), achievementID, progression);
    }

    @Override
    public void submitScoreToLeaderboard(String leaderboardID, int score) {
        Games.Leaderboards.submitScore(gameHelper.getApiClient(), leaderboardID, score);
    }

    @Override
    public void displayLeaderboard(String leaderboardID) {
        startActivityForResult(Games.Leaderboards.getLeaderboardIntent(gameHelper.getApiClient(), leaderboardID), 101);
    }

    @Override
    public void shareScore(String challenge, int score) {
        Intent sendIntent = new Intent();
        sendIntent.setAction(Intent.ACTION_SEND);
        sendIntent.putExtra(Intent.EXTRA_TEXT, "I managed to get "+score+" taps during the " + challenge + " while playing Tap Out! How fast are you? Challenge the world. #FMEGTapout Download Now! http://bit.ly/FMEGTapout");
        sendIntent.setType("text/plain");
        startActivity(sendIntent);
    }

}

上面的代码可以编译并正常运行,但是,当您尝试使用一种Google Play服务(例如更新乐谱或成就)时,会出现以下错误:
05-24 01:19:11.413: E/AndroidRuntime(24280): FATAL EXCEPTION: Timer-0
05-24 01:19:11.413: E/AndroidRuntime(24280): java.lang.IllegalStateException: GoogleApiClient is not connected yet.
05-24 01:19:11.413: E/AndroidRuntime(24280):    at com.google.android.gms.internal.fq.a(Unknown Source)
05-24 01:19:11.413: E/AndroidRuntime(24280):    at com.google.android.gms.common.api.b.b(Unknown Source)
05-24 01:19:11.413: E/AndroidRuntime(24280):    at com.google.android.gms.games.internal.api.AchievementsImpl.unlock(Unknown Source)
05-24 01:19:11.413: E/AndroidRuntime(24280):    at com.fmeg.tapout.android.AndroidLauncher.unlockAchievement(AndroidLauncher.java:77)
05-24 01:19:11.413: E/AndroidRuntime(24280):    at com.fmeg.tapout.screens.GameOver.handleAchievements(GameOver.java:98)
05-24 01:19:11.413: E/AndroidRuntime(24280):    at com.fmeg.tapout.screens.GameOver.<init>(GameOver.java:36)
05-24 01:19:11.413: E/AndroidRuntime(24280):    at com.fmeg.tapout.screens.Tick.run(Play.java:85)
05-24 01:19:11.413: E/AndroidRuntime(24280):    at java.util.Timer$TimerImpl.run(Timer.java:284)

如果您取消注释以下行,则同样在AndroidLauncher.java中
@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(请求,响应,数据);
}

该应用程序将在启动时崩溃,并且无论如何这都是根据我一直在使用的引用进行调用的方式。崩溃错误显示:
05-24 01:32:19.854: E/AndroidRuntime(25552): FATAL EXCEPTION: main
05-24 01:32:19.854: E/AndroidRuntime(25552): java.lang.IllegalStateException: A fatal developer error has occurred. Check the logs for further information.
05-24 01:32:19.854: E/AndroidRuntime(25552):    at com.google.android.gms.internal.ff$h.b(Unknown Source)
05-24 01:32:19.854: E/AndroidRuntime(25552):    at com.google.android.gms.internal.ff$h.a(Unknown Source)
05-24 01:32:19.854: E/AndroidRuntime(25552):    at com.google.android.gms.internal.ff$b.eN(Unknown Source)
05-24 01:32:19.854: E/AndroidRuntime(25552):    at com.google.android.gms.internal.ff$a.handleMessage(Unknown Source)
05-24 01:32:19.854: E/AndroidRuntime(25552):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-24 01:32:19.854: E/AndroidRuntime(25552):    at android.os.Looper.loop(Looper.java:137)
05-24 01:32:19.854: E/AndroidRuntime(25552):    at android.app.ActivityThread.main(ActivityThread.java:5455)
05-24 01:32:19.854: E/AndroidRuntime(25552):    at java.lang.reflect.Method.invokeNative(Native Method)
05-24 01:32:19.854: E/AndroidRuntime(25552):    at java.lang.reflect.Method.invoke(Method.java:525)
05-24 01:32:19.854: E/AndroidRuntime(25552):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
05-24 01:32:19.854: E/AndroidRuntime(25552):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
05-24 01:32:19.854: E/AndroidRuntime(25552):    at dalvik.system.NativeStart.main(Native Method)

https://github.com/TheInvader360/libgdx-gameservices-tutorial/blob/master/tutorial-libgdx-gameservices-android/src/com/theinvader360/tutorial/libgdx/gameservices/MainActivity.java

https://developers.google.com/games/services/android/leaderboards

最佳答案

您的代码看起来不错。但是,由于您使用的是CLIENT_ALL(意味着所有Play游戏服务),因此您需要将清单中的和都添加到清单中:

<meta-data android:name="com.google.android.gms.games.APP_ID" android:value="@string/app_id" />
<meta-data android:name="com.google.android.gms.appstate.APP_ID" android:value="@string/app_id" />

第一个需要登录,第二个需要云保存-即使您不使用它,也需要使用CLIENT_ALL进行访问。

这也使我感到震惊,因为第二个是,仅在文档的“云保存”部分提到了

如果这不能解决您的问题(即清单中已经包含了这些问题),那么最好的起点是文档的Troubleshooting部分。

另外,要获取完整的错误日志记录,应启用游戏助手的调试日志,如下所示:
 gameHelper = new GameHelper(this, GameHelper.CLIENT_ALL);
 gameHelper.setup(this);

 gameHelper.enableDebugLog(true);   // add this (but only for debug builds)

关于java - GameHelper在应用程序启动时崩溃,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23841976/

10-10 23:02