Android GooglePlayServices(游戏)连接崩溃

Android crash on GooglePlayServices (Games) connect

我在这个问题上纠结了大约 10 个小时,我不知道我做错了什么....

Fatal Exception:

E/AndroidRuntime: FATAL EXCEPTION: main
   Process: onl.deepspace.zoorallye, PID: 13256
   java.lang.IllegalStateException: A fatal developer error has occurred. Check the logs for further information.
   at com.google.android.gms.common.internal.zzj$zza.zzc(Unknown Source)
   at com.google.android.gms.common.internal.zzj$zza.zzw(Unknown Source)
   at com.google.android.gms.common.internal.zzj$zzc.zzqN(Unknown Source)
   at com.google.android.gms.common.internal.zzj$zzb.handleMessage(Unknown Source)
   at android.os.Handler.dispatchMessage(Handler.java:102)
   at android.os.Looper.loop(Looper.java:135)
   at android.app.ActivityThread.main(ActivityThread.java:5569)
   at java.lang.reflect.Method.invoke(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:372)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:931)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:726)

Log没有提供任何重要信息


我环顾四周 google,我可以修复一些错误。但是应用程序 总是 在这里崩溃:

achievements.googleApiClient.connect();

MainActivity(重要部分):

Achievements achievements;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ...

    //Init Achievements
    achievements = new Achievements(this);
    achievements.googleApiClient.connect(); //**AppCrash**
}

@Override
protected void onStart() {
    super.onStart();
}

@Override
protected void onStop() {
    super.onStop();
    achievements.googleApiClient.disconnect();
}    

Achievements.java:

public class Achievements implements GoogleApiClient.OnConnectionFailedListener, GoogleApiClient.ConnectionCallbacks{

public GoogleApiClient googleApiClient;
private Activity activity;

public Achievements(Activity activity){
    this.activity = activity;

    googleApiClient = new GoogleApiClient.Builder(activity)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(Games.API)
            .addScope(Games.SCOPE_GAMES)
            .build();
}

private void signIn(GoogleApiClient signinClient) {
    Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(signinClient);
    activity.startActivityForResult(signInIntent, 0);
}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
    if(connectionResult.getErrorCode() == ConnectionResult.SIGN_IN_REQUIRED){
        signIn(Tools.getSigninClient(activity));
    }
    Toast.makeText(activity, connectionResult.getErrorMessage(), Toast.LENGTH_SHORT).show();
    Log.e(Const.LOGTAG, connectionResult.getErrorMessage());
}

@Override
public void onConnected(@Nullable Bundle bundle) {
    Log.i(Const.LOGTAG, "Play Service (Games) Connected");
}

@Override
public void onConnectionSuspended(int i) {
}
}

AndroidManifest.xml:

     <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="onl.deepspace.zoorallye">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/appicon"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.Drawer"></activity>

    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
    <meta-data
        android:name="com.google.android.gms.games.onl.deepspace.zoorallye"
        android:value="@string/google_app_id" />

</application>

我认为这可能与您的 AndroidManifest.xml

中缺少 com.google.android.gms.games.APP_ID 元数据元素有关

它看起来像一个错字(你用你的包名替换了 APP_ID?)。它应该是: <meta-data android:name="com.google.android.gms.games.APP_ID" android:value="@string/app_id" />

可以参考TypeANumber基础示例的AndroidManifest.xml https://github.com/playgameservices/android-basic-samples/blob/master/BasicSamples/TypeANumber/src/main/AndroidManifest.xml#L41

在社区中寻找类似的帖子,发现了这个 answer,请检查一下。开门见山,我认为这个问题的原因在于你的AndroidManifest.xml。如答案中所述,应添加 <meta-data> 标签,您的标签在那里,但名称不同。显示为:

android:name="com.google.android.gms.games.onl.deepspace.zoorallye"

而在上面的答案中,它显示:

android:name="com.google.android.gms.games.APP_ID"

前往 Google Quickstart samples 并注意所有标签都与上面类似。在您的代码中没有发现任何其他导致问题的原因。