GameHelper在应用程序启动时崩溃

时间:2014-05-24 06:28:00

标签: java android google-play libgdx google-play-services

所以,我想它有助于说我使用的是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

1 个答案:

答案 0 :(得分:6)

您的代码看起来很好。但是,由于您使用的是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

这也让我感到高兴,因为第二个在文档的Cloud Save部分中提及。

如果这不能解决您的问题(即您的清单中已有问题),最好的起点是文档的Troubleshooting部分。

此外,要获得完整的错误记录,您应该启用游戏助手的调试日志,如下所示:

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

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