Android: ошибка при вызове GoogleApiClient.connect ()

Когда я вызываю метод mGoogleApiClient.connect (), приложение вылетает.

следующий код скопирован с www.developers.google.com

Я хочу реализовать функцию входа в Google, но не могу.

Я также редактировал файл build.gradle. в моем последнем приложении я не мог добавить списки лидеров только из-за этой проблемы, и теперь моему следующему приложению снова нужны списки лидеров, но на этот раз я не могу их оставить.

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.drive.Drive;
import com.google.android.gms.games.Games;
import com.google.example.games.basegameutils.BaseGameUtils;

public class MenuActivity extends Activity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, View.OnClickListener {
    boolean mExplicitSignOut = false;
    boolean mInSignInFlow = false; // set to true when you're in the middle of the
    // sign in flow, to know you should not attempt
    // to connect in onStart()
    GoogleApiClient mGoogleApiClient;  // initialized in onCreate
    private static int RC_SIGN_IN = 9001;
    private boolean mResolvingConnectionFailure = false;
    private boolean mAutoStartSignInFlow = true;
    private boolean mSignInClicked = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_menu);
        findViewById(R.id.sign_in_button).setOnClickListener(this);
        findViewById(R.id.sign_out_button).setOnClickListener(this);

        // Create the Google Api Client with access to the Play Game and Drive services.
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(Games.API).addScope(Games.SCOPE_GAMES)
                .addApi(Drive.API).addScope(Drive.SCOPE_APPFOLDER) // Drive API
                .build();
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (!mInSignInFlow && !mExplicitSignOut) {
            // auto sign in
            //mGoogleApiClient.connect();
        }

        //checkButtonVisibility();
    }

    private void checkButtonVisibility() {
        if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
            findViewById(R.id.sign_out_button).setVisibility(View.VISIBLE);
            // signed in. Show the "sign out" button and explanation.
            // ...
        } else {
            findViewById(R.id.sign_in_button).setVisibility(View.VISIBLE);
            // not signed in. Show the "sign in" button and explanation.
            // ...
        }
    }


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

    @Override
    public void onConnected(Bundle connectionHint) {
        // show sign-out button, hide the sign-in button
        findViewById(R.id.sign_in_button).setVisibility(View.GONE);
        findViewById(R.id.sign_out_button).setVisibility(View.VISIBLE);

        // (your code here: update UI, enable functionality that depends on sign in, etc)
    }



    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        if (mResolvingConnectionFailure) {
            // Already resolving
            return;
        }

        // If the sign in button was clicked or if auto sign-in is enabled,
        // launch the sign-in flow
        if (mSignInClicked || mAutoStartSignInFlow) {
            mAutoStartSignInFlow = false;
            mSignInClicked = false;
            mResolvingConnectionFailure = true;

            // Attempt to resolve the connection failure using BaseGameUtils.
            // The R.string.signin_other_error value should reference a generic
            // error string in your strings.xml file, such as "There was
            // an issue with sign in, please try again later."
            if (!BaseGameUtils.resolveConnectionFailure(this, mGoogleApiClient, connectionResult,
                    RC_SIGN_IN, "signin_other_error")) //R.string.signin_other_error
            {
                mResolvingConnectionFailure = false;
            }
        }

        // Put code here to display the sign-in button
    }

    @Override
    public void onConnectionSuspended(int i) {
        // Attempt to reconnect
        mGoogleApiClient.connect();
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        if (requestCode == RC_SIGN_IN) {
            mSignInClicked = false;
            mResolvingConnectionFailure = false;
            if (resultCode == RESULT_OK) {
                mGoogleApiClient.connect();
            } else {
                // Bring up an error dialog to alert the user that sign-in
                // failed. The R.string.signin_failure should reference an error
                // string in your strings.xml file that tells the user they
                // could not be signed in, such as "Unable to sign in."
                BaseGameUtils.showActivityResultError(this, requestCode, resultCode, RC_SIGN_IN); //R.string.signin_failure
            }
        }
        //checkButtonVisibility();
    }

    // Call when the sign-in button is clicked
    private void signInClicked() {
        mSignInClicked = true;
        mGoogleApiClient.connect();
    }

    // Call when the sign-out button is clicked
    private void signOutclicked() {
        mSignInClicked = false;
        Games.signOut(mGoogleApiClient);
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.sign_out_button) {
            // user explicitly signed out, so turn off auto sign in
            mExplicitSignOut = true;
            if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
                Games.signOut(mGoogleApiClient);
                mGoogleApiClient.disconnect();
            }
        }

        if (view.getId() == R.id.sign_in_button) {
            // start the asynchronous sign in flow
            mSignInClicked = true;
            mGoogleApiClient.connect();
        }
        else if (view.getId() == R.id.sign_out_button) {
            // sign out.
            mSignInClicked = false;
            Games.signOut(mGoogleApiClient);

            // show sign-in button, hide the sign-out button
            findViewById(R.id.sign_in_button).setVisibility(View.VISIBLE);
            findViewById(R.id.sign_out_button).setVisibility(View.GONE);
        }

        checkButtonVisibility();
    }
}

Logcat:

06-12 09:54:03.113    4364-4364/com.example.gSignIn I/System.out﹕ Calling Method: mGoogleApiClient.connect( )
06-12 09:54:03.238    4364-4368/com.example.gSignIn D/dalvikvm﹕ GC_CONCURRENT freed 1903K, 16% free 14093K/16647K, paused 21ms+6ms, total 63ms
06-12 09:54:03.598    4364-4364/com.example.gSignIn D/AndroidRuntime﹕ Shutting down VM
06-12 09:54:03.598    4364-4364/com.example.gSignIn W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x411762a0)
06-12 09:54:03.613    4364-4364/com.example.gSignIn E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.IllegalStateException: A fatal developer error has occurred. Check the logs for further information.
            at com.google.android.gms.internal.jl$h.b(Unknown Source)
            at com.google.android.gms.internal.jl$h.g(Unknown Source)
            at com.google.android.gms.internal.jl$b.hy(Unknown Source)
            at com.google.android.gms.internal.jl$a.handleMessage(Unknown Source)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4921)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
            at dalvik.system.NativeStart.main(Native Method)

Манифест:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.gSignIn">

    <application android:allowBackup="true" android:label="@string/app_name"
        android:icon="@drawable/ic_launcher" android:theme="@style/AppTheme">

    <activity android:name="com.example.gSignIn.Splash" android:label="@string/app_name" android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>

    <activity android:name="com.example.gSignIn.MenuActivity" android:label="@string/app_name" android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="com.example.gSignIn.MenuActivity"/>
            <category android:name="android.intent.category.DEFAULT"/>
        </intent-filter>
    </activity>


    </application>
</manifest>

person Usman    schedule 12.06.2015    source источник
comment
Пожалуйста, добавьте трассировку стека ошибки из вашего logcat   -  person Jayesh Elamgodil    schedule 12.06.2015
comment
Не могли бы вы также добавить свой файл манифеста   -  person Jayesh Elamgodil    schedule 12.06.2015
comment
Я думаю, что у меня проблема, я не добавил разрешение.   -  person Usman    schedule 12.06.2015
comment
вы создали проект в консоли разработчика?   -  person Mightian    schedule 12.06.2015
comment
все, что у вас есть проблема, это добавить свой app_id .. упомянутый в ссылке ответа, которую я дал ..   -  person Ajay P. Prajapati    schedule 12.06.2015
comment
Спасибо «Джайеш Эламгодил», «Аджай П. Праджапати» и «war_Hero» за потраченное время.   -  person Usman    schedule 12.06.2015
comment
Не удалось войти. Проверьте подключение к сети и повторите попытку. но другие игры работают отлично, их таблицы лидеров тоже обновляются.   -  person Usman    schedule 12.06.2015


Ответы (2)


Несколько вещей, которые стоит проверить:

  1. Правильно ли вы настроили проект консоли API в консоли разработчика Google?
  2. Если вы используете игровые сервисы Google Play, вы должны настроить проект с помощью Консоль игр Google Play (нажмите  введите описание изображения здесь значок слева).
  3. Если вы используете игровые сервисы Google Play, вам также может потребоваться добавить следующее в свой androidmanifest.xml :

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

Ваш идентификатор приложения из консоли игровых сервисов Google Play должен быть установлен в resources / values.xml, Например:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_id">1234567890</string>
</resources>

Обратите внимание, что идентификатор приложения - это просто уникальное значение из 10 символов, а не более длинная строка, которая является идентификатором клиента.

person class    schedule 12.06.2015
comment
когда я вызываю mGoogleApiClient.connect (), ничего не происходит, когда я вызываю mGoogleApiClient.isConnected (), результат будет «ложным». - person Usman; 16.06.2015

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

Это помогло мне, также убедитесь, что вы правильно ссылаетесь на свой build.gradle: Добавьте в зависимости: compile project (': BaseGameUtils')

person user3856297    schedule 14.03.2016