libGDX Google Play游戏服务 - Android

时间:2014-03-13 07:29:40

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

我正在尝试将Google Play游戏服务实施到libGDX项目中。 在我的android项目中,MainActivity看起来像这样:

public class MainActivity extends AndroidApplication implements
    GameHelper.GameHelperListener, ActionResolver {

GameHelper gameHelper;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    gameHelper = new GameHelper(this, GameHelper.CLIENT_ALL);
    gameHelper.enableDebugLog(true);

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);

    AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
    cfg.useGL20 = false;

    initialize(new CJSGame(this), cfg);
    gameHelper.setup(this);

}

@Override
public void onStart() {
    super.onStart();
    gameHelper.onStart(this);
}

@Override
public void onStop() {
    super.onStop();
    gameHelper.onStop();
}

    @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    gameHelper.onActivityResult(requestCode, resultCode, data);
}

@Override
public boolean getSignedInGPGS() {
    // TODO Auto-generated method stub
    return gameHelper.isSignedIn();
}

@Override
public void loginGPGS() {
    // TODO Auto-generated method stub
    try {
        runOnUiThread(new Runnable() {
            public void run() {
                gameHelper.beginUserInitiatedSignIn();
            }
        });
    } catch (final Exception ex) {
    }
}

@Override
public void submitScoreGPGS(int score) {
    // TODO Auto-generated method stub
    Games.Leaderboards.submitScore(gameHelper.getApiClient(),
            getString(R.string.leaderboard1_id), score);
}

@Override
public void unlockAchievementGPGS(String achievementId) {
    // TODO Auto-generated method stub
    Games.Achievements.unlock(gameHelper.getApiClient(), achievementId);
}

@Override
public void getLeaderboardGPGS() {
    // TODO Auto-generated method stub
    startActivityForResult(
            Games.Leaderboards.getLeaderboardIntent(
                    gameHelper.getApiClient(),
                    getString(R.string.leaderboard1_id)), 100);
}

@Override
public void getAchievementsGPGS() {
    // TODO Auto-generated method stub
    startActivityForResult(
            Games.Achievements.getAchievementsIntent(gameHelper
                    .getApiClient()), 101);
}

@Override
public void onSignInFailed() {
    // TODO Auto-generated method stub
    System.out.println("Sign in failed");
}

@Override
public void onSignInSucceeded() {
    // TODO Auto-generated method stub
    System.out.println("Sign in succeeded");
}
}

当我尝试在Android设备上运行应用程序时,我最终在logcat中出现IllegalStateException错误:

03-13 10:23:40.766: E/AndroidRuntime(31339): FATAL EXCEPTION: main
03-13 10:23:40.766: E/AndroidRuntime(31339): java.lang.IllegalStateException: A fatal developer error has occurred. Check the logs for further information.
03-13 10:23:40.766: E/AndroidRuntime(31339):    at com.google.android.gms.internal.dw$h.b(Unknown Source)
03-13 10:23:40.766: E/AndroidRuntime(31339):    at  com.google.android.gms.internal.dw$h.b(Unknown Source)
03-13 10:23:40.766: E/AndroidRuntime(31339):    at com.google.android.gms.internal.dw$b.bR(Unknown Source)
03-13 10:23:40.766: E/AndroidRuntime(31339):    at com.google.android.gms.internal.dw$a.handleMessage(Unknown Source)
03-13 10:23:40.766: E/AndroidRuntime(31339):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-13 10:23:40.766: E/AndroidRuntime(31339):    at android.os.Looper.loop(Looper.java:137)
03-13 10:23:40.766: E/AndroidRuntime(31339):    at android.app.ActivityThread.main(ActivityThread.java:5419)
03-13 10:23:40.766: E/AndroidRuntime(31339):    at java.lang.reflect.Method.invokeNative(Native Method)
03-13 10:23:40.766: E/AndroidRuntime(31339):    at java.lang.reflect.Method.invoke(Method.java:525)
03-13 10:23:40.766: E/AndroidRuntime(31339):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
03-13 10:23:40.766: E/AndroidRuntime(31339):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
03-13 10:23:40.766: E/AndroidRuntime(31339):    at dalvik.system.NativeStart.main(Native Method)

任何帮助吗??

编辑:我已经在清单文件中有元数据标签

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

1 个答案:

答案 0 :(得分:7)

解决了,我启用了adb logcat,发现清单文件中缺少另一个元数据标记:

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

刚刚添加了此标记,它正常运行。