问题
So, I guess it'd help to say that I'm using LibGDX.
Code:
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);
}
}
The above code compiles and runs fine, however as soon as you attempt to use one of the google play services, such as updating a score or an achievement you get this error:
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)
Also in AndroidLauncher.java if you uncomment the following lines
@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); }
The application will crash upon startup, and this is how it's supposed to be called anyway according to the references I've been using. The crash error shows:
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:
Your code looks fine. However, since you are using CLIENT_ALL
(meaning all Play Games services) you will need to add both of the following to your manifest:
<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" />
The first is required to sign-in, and the second is required for cloud saves - which, even if you don't use it, you have requested access to with CLIENT_ALL
.
This tripped me up as well because the second one is only mentioned under the Cloud Save section of the documentation.
If this does not fix your problem (i.e. you already have those in your manifest), the best place to start is the Troubleshooting section of the documentation.
Also, to get full error logging you should enable the debug log for the game helper, as follows:
gameHelper = new GameHelper(this, GameHelper.CLIENT_ALL);
gameHelper.setup(this);
gameHelper.enableDebugLog(true); // add this (but only for debug builds)
来源:https://stackoverflow.com/questions/23841976/gamehelper-crashing-at-application-start