大家好我登录google plus时遇到问题,当我点击登录时没有任何反应。
我说我导入了谷歌API,我打开了网站上的API。
public class GooglePlusActivity extends Activity implements ConnectionCallbacks, OnConnectionFailedListener {
private static final int RC_SIGN_IN = 0;
// Google client to communicate with Google
private GoogleApiClient mGoogleApiClient;
private boolean mIntentInProgress;
private boolean mSignInClicked;
private ConnectionResult mConnectionResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(Plus.API, Plus.PlusOptions.builder().build())
.addScope(Plus.SCOPE_PLUS_LOGIN).build();
}
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
googlePlusLogin();
}
protected void onStop() {
super.onStop();
if (mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
}
private void resolveSignInError() {
if (mConnectionResult.hasResolution()) {
try {
mIntentInProgress = true;
mConnectionResult.startResolutionForResult(this, RC_SIGN_IN);
} catch (SendIntentException e) {
mIntentInProgress = false;
mGoogleApiClient.connect();
}
}
}
@Override
public void onConnectionFailed(ConnectionResult result) {
if (!result.hasResolution()) {
GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(), this,
0).show();
return;
}
if (!mIntentInProgress) {
// Store the ConnectionResult for later usage
mConnectionResult = result;
if (mSignInClicked) {
googlePlusLogin();
resolveSignInError();
}
}
}
@Override
protected void onActivityResult(int requestCode, int responseCode, Intent intent) {
if (requestCode == RC_SIGN_IN) {
if (responseCode != RESULT_OK) {
mSignInClicked = false;
}
mIntentInProgress = false;
if (!mGoogleApiClient.isConnecting()) {
mGoogleApiClient.connect();
}
}
}
public void onResume()
{
super.onResume();
finish();
}
@Override
public void onConnected(Bundle arg0) {
mSignInClicked = false;
Toast.makeText(this, "Connected", Toast.LENGTH_LONG).show();
getProfileInformation();
}
private void getProfileInformation() {
try {
if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) {
Person currentPerson = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
String personName = currentPerson.getDisplayName();
String personPhotoUrl = currentPerson.getImage().getUrl();
String email = Plus.AccountApi.getAccountName(mGoogleApiClient);
Toast.makeText(getApplicationContext(), "Benvenuto " + email + ":" + currentPerson, Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onConnectionSuspended(int cause) {
mGoogleApiClient.connect();
}
public void googlePlusLogin() {
if (!mGoogleApiClient.isConnecting()) {
mSignInClicked = true;
resolveSignInError();
}
}
private void googlePlusLogout() {
if (mGoogleApiClient.isConnected()) {
Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
mGoogleApiClient.disconnect();
mGoogleApiClient.connect();
}
}
}
这是开始活动
if(v.getId() == R.id.sign_in_button) {
Intent plus = new Intent(this, GooglePlusActivity.class);
startActivity(plus);
}
这是logcat
03-15 20:55:09.847: W/System.err(22572): java.lang.IllegalStateException: GoogleApiClient must be connected.
03-15 20:55:09.847: W/System.err(22572): at com.google.android.gms.internal.jx.a(Unknown Source)
03-15 20:55:09.847: W/System.err(22572): at com.google.android.gms.plus.Plus.a(Unknown Source)
03-15 20:55:09.847: W/System.err(22572): at com.google.android.gms.internal.pc.getCurrentPerson(Unknown Source)
03-15 20:55:09.847: W/System.err(22572): at it.activity.GooglePlusActivity.getProfileInformation(GooglePlusActivity.java:111)
03-15 20:55:09.847: W/System.err(22572): at it.activity.GooglePlusActivity.onStart(GooglePlusActivity.java:37)
03-15 20:55:09.847: W/System.err(22572): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1220)
03-15 20:55:09.847: W/System.err(22572): at android.app.Activity.performStart(Activity.java:5993)
03-15 20:55:09.847: W/System.err(22572): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2268)
03-15 20:55:09.847: W/System.err(22572): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2367)
03-15 20:55:09.847: W/System.err(22572): at android.app.ActivityThread.access$800(ActivityThread.java:148)
03-15 20:55:09.847: W/System.err(22572): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)
03-15 20:55:09.847: W/System.err(22572): at android.os.Handler.dispatchMessage(Handler.java:102)
03-15 20:55:09.847: W/System.err(22572): at android.os.Looper.loop(Looper.java:135)
03-15 20:55:09.848: W/System.err(22572): at android.app.ActivityThread.main(ActivityThread.java:5274)
03-15 20:55:09.848: W/System.err(22572): at java.lang.reflect.Method.invoke(Native Method)
03-15 20:55:09.848: W/System.err(22572): at java.lang.reflect.Method.invoke(Method.java:372)
03-15 20:55:09.848: W/System.err(22572): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:909)
03-15 20:55:09.848: W/System.err(22572): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704)
03-15 20:55:09.848: W/System.err(22572): java.lang.IllegalStateException: GoogleApiClient must be connected.
03-15 20:55:09.848: W/System.err(22572): at com.google.android.gms.internal.jx.a(Unknown Source)
03-15 20:55:09.848: W/System.err(22572): at com.google.android.gms.plus.Plus.a(Unknown Source)
03-15 20:55:09.848: W/System.err(22572): at com.google.android.gms.internal.pc.getCurrentPerson(Unknown Source)
03-15 20:55:09.848: W/System.err(22572): at it.activity.GooglePlusActivity.getProfileInformation(GooglePlusActivity.java:111)
03-15 20:55:09.848: W/System.err(22572): at it.activity.GooglePlusActivity.onResume(GooglePlusActivity.java:96)
03-15 20:55:09.848: W/System.err(22572): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1241)
03-15 20:55:09.848: W/System.err(22572): at android.app.Activity.performResume(Activity.java:6063)
03-15 20:55:09.848: W/System.err(22572): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2947)
03-15 20:55:09.848: W/System.err(22572): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2989)
03-15 20:55:09.848: W/System.err(22572): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2372)
03-15 20:55:09.848: W/System.err(22572): at android.app.ActivityThread.access$800(ActivityThread.java:148)
03-15 20:55:09.848: W/System.err(22572): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)
03-15 20:55:09.848: W/System.err(22572): at android.os.Handler.dispatchMessage(Handler.java:102)
03-15 20:55:09.848: W/System.err(22572): at android.os.Looper.loop(Looper.java:135)
03-15 20:55:09.848: W/System.err(22572): at android.app.ActivityThread.main(ActivityThread.java:5274)
03-15 20:55:09.849: W/System.err(22572): at java.lang.reflect.Method.invoke(Native Method)
03-15 20:55:09.849: W/System.err(22572): at java.lang.reflect.Method.invoke(Method.java:372)
03-15 20:55:09.849: W/System.err(22572): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:909)
03-15 20:55:09.849: W/System.err(22572): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704)
答案 0 :(得分:1)
你的问题是:
if (!mGoogleApiClient.isConnecting()) {
mSignInClicked = true;
resolveSignInError();
}
您目前的流量如下:
然后,您将获得:onConnectionFailedCallback
,如果是mSignInClicked == true
,请检查是否为public void googlePlusLogin() {
mSignInClicked = true;
}
,尝试解决错误。
将您的googlePlusLogin更改为
{{1}}
此错误的根本原因是您使用的代码对于用户交互式登录大多是正确的,并尝试使其适用于活动创建。差异纯粹是时间。如果人类按下按钮(而不是onStart)调用了googlePlusLogin,则可能没问题,因为GoogleApiClient可能已完成连接设置。通过在活动开始时立即执行此操作,您几乎可以保证它不会被连接,在这种情况下,您将坚持使用您的代码。
答案 1 :(得分:0)
它表示您与mGoogleApiClient无关。由于您没有连接,请在此处添加mGoogleApiClient.isConnected()。
将这些线路连接到
googlePlusLogin();
getProfileInformation();
到onConnected方法。
您必须在Google控制台“凭据”部分注册您的应用程序。因此,您可能正在使用不同的密钥对您的应用程序进行签名,然后使用已添加到Google控制台的密钥。