Android startResolutionForResult返回错误(应用程序崩溃),GoogleAppClient connect

Android startResolutionForResult返回错误(应用程序崩溃),GoogleAppClient connect,android,google-api-client,Android,Google Api Client,很长一段时间以来,我一直在想如何解决这个问题,但我找不到任何关于问题所在的好信息 代码源于: 因此,首先我构建GoogleAppClient对象,然后onStart()尝试连接它。 连接失败,我最终进入了“onConnectionFailed()” 这里我已经打印了结果,这是我得到的:“需要签名” 由于该错误具有解决方案,因此将调用“result.startResolutionForResult(this,REQUEST_RESOLVE_error)” 这就是程序突然崩溃的地方 任何帮助都会被感

很长一段时间以来,我一直在想如何解决这个问题,但我找不到任何关于问题所在的好信息

代码源于:

因此,首先我构建GoogleAppClient对象,然后onStart()尝试连接它。 连接失败,我最终进入了“onConnectionFailed()”

这里我已经打印了结果,这是我得到的:“需要签名”

由于该错误具有解决方案,因此将调用“result.startResolutionForResult(this,REQUEST_RESOLVE_error)”

这就是程序突然崩溃的地方

任何帮助都会被感激的,我对android开发还很陌生,所以对我放松点

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.IntentSender.SendIntentException;
import android.util.Log;
import android.view.Menu;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.plus.Plus;

public class MainActivity extends Activity implements
ConnectionCallbacks, OnConnectionFailedListener  {

    /* Client used to interact with Google APIs. */
    private GoogleApiClient mGoogleApiClient;
    // Request code to use when launching the resolution activity
    private static final int REQUEST_RESOLVE_ERROR = 1001;
    // Bool to track whether the app is already resolving an error
    private boolean mResolvingError = false;

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

        mGoogleApiClient = new GoogleApiClient.Builder(this)
        .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(Plus.API, null)
                .addScope(Plus.SCOPE_PLUS_LOGIN)
                .build();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    protected void onStart() {
        super.onStart();
        mGoogleApiClient.connect();
    }
    protected void onStop() {
        super.onStop();

        if (mGoogleApiClient.isConnected()) {
            mGoogleApiClient.disconnect();
        }
    }

    @Override
    public void onConnectionFailed(ConnectionResult result) {
        if (mResolvingError) {
            // Already attempting to resolve an error.
            return;
        } else if (result.hasResolution()) {
            Log.d("MyApp",result.toString());
            try {
                mResolvingError = true;
                result.startResolutionForResult(this, REQUEST_RESOLVE_ERROR);
            } catch (SendIntentException e) {
                // There was an error with the resolution intent. Try again.
                mGoogleApiClient.connect();
            }
        } else {
            // Show dialog using GooglePlayServicesUtil.getErrorDialog()
            //showErrorDialog(result.getErrorCode());
            mResolvingError = true;
        }
    }

    @Override
    public void onConnected(Bundle connectionHint) {

    }

    @Override
    public void onConnectionSuspended(int cause) {
        mGoogleApiClient.connect();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_RESOLVE_ERROR) {
            mResolvingError = false;
            if (resultCode == RESULT_OK) {
                // Make sure the app is not already connected or attempting to connect
                if (!mGoogleApiClient.isConnecting() && !mGoogleApiClient.isConnected()) {
                    mGoogleApiClient.connect();
                }
            }
        }
    }
}

onCreate
方法中,尝试:

  • 删除重复的行
    super.onCreate(savedInstanceState)
  • 删除
    .addScope(Plus.SCOPE\u Plus\u LOGIN)
    设置范围(“Plus\u LOGIN”)
如果这没有帮助,请尝试从SDK示例导入并运行伟大的示例项目:

<android-sdk-folder>/extras/google/google_play_services/

您能从
adb logcat
打印stacktrace吗?我收到了同样的问题。在onConnectionFailed()方法中获取ConnectionResult=null.PS:您不需要在onConnectionSuspended方法中再次调用connect(),因为如果成功,它将自动尝试重新连接并调用onConnected()。