应用程序在mGoogleApiClient.connect()时崩溃;叫做

时间:2015-06-09 21:23:16

标签: android

我正在尝试为我的应用集成登录功能。但是当按下登录按钮时它会崩溃。

日志显示单击了Signin按钮,其中mGoogleApiClient.connect();被叫,然后它崩溃了。此外,没有任何登录覆盖方法,所以它不会进入任何连接方法。所以我无法弄清楚为什么登录崩溃。是否有任何其他@Override方法,我需要实施

06-09 16:44:05.820  13307-13307/com.appingapps.narayan.mancalapp D/Mancala﹕      
Sign-in button clicked

W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception 06-09 16:44:05.895  13307-13307/com.appingapps.narayan.mancalapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.IllegalStateException: A fatal developer error has occurred.

我的相关代码如下

public class MainActivity extends Activity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {    .....
@Override
protected void onCreate(Bundle savedInstanceState) {
...
// Create the Google Api Client with access to Plus and Games
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(Plus.API).addScope(Plus.SCOPE_PLUS_LOGIN)
            .addApi(Games.API).addScope(Games.SCOPE_GAMES)
            .build();
...
}
 protected void onStart() {
   Log.d(TAG, "onStart()");
    super.onStart();
   // mGoogleApiClient.connect();
}

protected void onStop() {
    Log.d(TAG, "onStop()");
    super.onStop();
    if (mGoogleApiClient.isConnected()) {
        mGoogleApiClient.disconnect();
    }
}

private void showSignInBar() {
    Log.d(TAG, "Showing sign in bar");
    Button b = (Button)findViewById(R.id.signin);
    b.setText("Sign In");
}

// Shows the "sign out" bar (explanation and button).
private void showSignOutBar() {
    Log.d(TAG, "Showing sign out bar");
    Button b = (Button)findViewById(R.id.signin);
    b.setText("Sign Out");
}

单击登录按钮时调用的方法

public void signin(){
    if(!mSignInClicked) {
        mGoogleApiClient.connect();
        Log.d(TAG, "Sign-in button clicked");
        mSignInClicked = true;
    }else{
        mSignInClicked = false;
        Games.signOut(mGoogleApiClient);
        mGoogleApiClient.disconnect();
        showSignInBar();
    }
}

覆盖方法

 @Override
 public void onConnected(Bundle bundle) {
     Log.d(TAG, "onConnected() called. Sign in successful!");
     showSignOutBar();
 }

@Override
 public void onConnectionSuspended(int i) {
     Log.d(TAG, "onConnectionSuspended() called. Trying to reconnect.");
     mGoogleApiClient.connect();
 }

 @Override
 public void onConnectionFailed(ConnectionResult connectionResult) {
     Log.d(TAG, "onConnectionFailed() called, result: " + connectionResult);

     if (mResolvingConnectionFailure) {
         Log.d(TAG, "onConnectionFailed() ignoring connection failure; already resolving.");
         return;
     }

     if (mSignInClicked || mAutoStartSignInFlow) {
         mAutoStartSignInFlow = false;
         mSignInClicked = false;
         mResolvingConnectionFailure = BaseGameUtils.resolveConnectionFailure(this, mGoogleApiClient,
                 connectionResult, RC_SIGN_IN, getString(R.string.signin_other_error));
     }
     showSignInBar();
 }
protected void onActivityResult(int requestCode, int responseCode, Intent intent) {
    Log.d(TAG, "in onActivityResult");
    if (requestCode == RC_SIGN_IN) {
        Log.d(TAG, "onActivityResult with requestCode == RC_SIGN_IN, responseCode="
                + responseCode + ", intent=" + intent);
        mSignInClicked = false;
        mResolvingConnectionFailure = false;
        if (responseCode == RESULT_OK) {
            mGoogleApiClient.connect();
        } else {
            BaseGameUtils.showActivityResultError(this,requestCode,responseCode, R.string.signin_other_error);
        }
    }
}

1 个答案:

答案 0 :(得分:6)

使用您在Manifest文件中从Google注册的app_id添加以下内容。

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

如果您也在使用此游戏,则可能还需要添加以下内容

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

这可能是唯一的原因。希望它有所帮助