简体   繁体   中英

Android GoogleApiClient application crash at connect()

my app crashes without any error message at googleApiClient.connect() . It never gets to onConnectionFailed. Here's what I have:

at MainActivity

public class MainMenu extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {

    public static final int REQUEST_LEADERBOARD = 1;
    public static int SCREEN_WIDTH;
    public static int SCREEN_HEIGHT;
    private GoogleApiClient googleApiClient;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);

        SCREEN_WIDTH = metrics.widthPixels;
        SCREEN_HEIGHT = metrics.heightPixels;

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

    public void newGame(View view) {
        Intent intent = new Intent(MainMenu.this, Game.class);
        startActivity(intent);
        finish();
    }

    public void highScore(View view) {
        if (isSignedIn()) {
            startActivityForResult(Games.Leaderboards.getLeaderboardIntent(googleApiClient,
                    String.valueOf(R.string.leaderboard_id)), REQUEST_LEADERBOARD);
        }
        else {
            System.out.println("not sign in");
        }
    }

    @Override
    public void onConnected(Bundle bundle) {

    }

    @Override
    public void onConnectionSuspended(int i) {

    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        System.out.println("failed");
    }

    @Override
    protected void onStart() {
        super.onStart();
        googleApiClient.connect(); //here it crashed
    }

    private boolean isSignedIn() {
        return (googleApiClient != null && googleApiClient.isConnected());
    }
}

What I've done:

I have generated signed APK, I have sha1 key. I published the app for alpha testing.

manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="my.package"
          android:versionCode="2"
          android:versionName="1.1">
    <uses-sdk android:minSdkVersion="1"/>



    <application android:icon="@drawable/ic_launcher" android:label="@string/app_name"
                 android:theme="@style/Theme.AppCompat.NoActionBar"
                 android:isGame="true" >

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


        <activity android:name=".activities.MainMenu">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <activity android:name=".activities.Game"/>
        <activity android:name=".activities.GameOver"/>
        <activity android:name=".activities.HighScore"/>
    </application>
</manifest>

I'm testing it on device with Google Play Service installed. Could you help me?

You should add the internet permission to your manifest.

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

If you need more help, you should show the error stacktrace.

Hope it helps you!

The solution is simple

<meta-data
    android:name="my.package.version"
    android:value="com.google.android.gms.version" />

is nonsense.

it should be:

<meta-data
     android:name="com.google.android.gms.version"
     android:value="@integer/google_play_services_version" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM