android - mGoogleApiClient.connect(); 时应用崩溃叫做

标签 android

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

日志仅显示在 mGoogleApiClient.connect() 处单击了登录按钮;被调用然后崩溃。也没有打印 Override 方法中的任何 Log,因此它不会进入任何连接方法。所以我无法弄清楚为什么登录崩溃。是否还有其他 @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);
        }
    }
}

最佳答案

使用您在 Manifest 文件中从谷歌注册的 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" />

这可能是唯一的原因。希望对你有帮助

关于android - mGoogleApiClient.connect(); 时应用崩溃叫做,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30743082/

相关文章:

android - 使用 Cordova 的 Azure 通知中心

android - 构建 ios 应用程序 Cordova 时找不到“Cordova/CDV.h”文件

java - 我无法将 Java 完美地转换为 Kotlin

Android - proguard 忽略 - 保持 Swig 方法

android - 同一textView中具有多种字体和textSize的字符串

java - 如何检测 libGdx 中的文本点击

java - 当应用程序崩溃时,Android App SharedPreferences 会被删除吗?

Android webview html5 视频自动播放不适用于 android 4.0.3

java - 为什么重新启动具有限制 UserManager.DISALLOW_USB_FILE_TRANSFER 使用的应用程序的设备会导致该设备无法使用?

android - Flutter 嵌套 ListView.builder 抛出错误