【问题标题】:startResolutionForResult returns an error (application crashes), GoogleApiClient connectstartResolutionForResult 返回错误(应用程序崩溃),GoogleApiClient 连接
【发布时间】:2014-02-22 19:29:38
【问题描述】:

我一直在试图弄清楚如何解决这个问题很长时间,但我找不到任何关于出了什么问题的好信息。

代码来源于:http://developer.android.com/google/auth/api-client.html

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

我在这里打印了结果,这是我得到的:“SIGN_IN_REQUIRED”

由于该错误有解决方案,因此将调用“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();
                }
            }
        }
    }
}

【问题讨论】:

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

标签: android google-api-client


【解决方案1】:

onCreate方法中尝试:

  • 删除重复行super.onCreate(savedInstanceState);
  • 删除 .addScope(Plus.SCOPE_PLUS_LOGIN).setScopes("PLUS_LOGIN") 就像我的情况一样

如果这没有帮助,请尝试从 SDK 示例中导入并运行出色的示例项目:

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

https://developers.google.com/+/mobile/android/getting-started

【讨论】:

    猜你喜欢
    • 2020-06-07
    • 2018-12-08
    • 2020-03-03
    • 2023-01-08
    • 1970-01-01
    • 1970-01-01
    • 2018-01-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多