gpt4 book ai didi

android - startResolutionForResult 返回错误(应用程序崩溃),GoogleApiClient 连接

转载 作者:行者123 更新时间:2023-11-30 03:04:57 37 4
gpt4 key购买 nike

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

代码来源于: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();
}
}
}
}
}

最佳答案

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

关于android - startResolutionForResult 返回错误(应用程序崩溃),GoogleApiClient 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21959349/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com