我一直试图弄清楚如何解决这个问题很长一段时间了,但我找不到任何关于出了什么问题的好信息。
代码来自: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();
}
}
}
}
}
答案 0 :(得分: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