Google 加上 Api 运行时异常
Google Plus Api Exception at runtime
大家好,我在登录 google 时遇到问题另外,当我点击登录时没有任何反应。
我声明我导入了 API google,我在网站上打开了 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();
}
}
}
这是开始Activity
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.access0(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.access0(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)
它说您没有连接到 mGoogleApiClient。由于您未连接,因此也请添加 mGoogleApiClient.isConnected() 检查那里。
移植这些行
googlePlusLogin();
getProfileInformation();
到 onConnected 方法。
您必须在 google 控制台 "credentials" 部分注册您的应用程序。因此,也许您使用的是与添加到 google 控制台的密钥不同的密钥来签署您的应用程序。
您的问题是:
if (!mGoogleApiClient.isConnecting()) {
mSignInClicked = true;
resolveSignInError();
}
您当前的流程如下所示:
- 连接 GoogleAPIClient
- 如果 GoogleAPIClient 未连接,mSignInClicked = true
然后您将得到:onConnectionFailedCallback
您检查是否 mSignInClicked == true
如果是,请尝试解决错误。
将您的 googlePlusLogin 更改为
public void googlePlusLogin() {
mSignInClicked = true;
}
此错误的根本原因是您使用的代码对于用户交互式登录来说大部分都是正确的,并试图使其在 activity 创建时起作用。区别纯粹是时间。如果您的 googlePlusLogin 是由人类按下按钮(而不是 onStart)调用的,那么它可能没问题,因为 GoogleApiClient 可能已经完成了连接设置。通过在 activity 启动后立即执行此操作,您几乎可以保证它不会被连接,在这种情况下,您将坚持使用现有的代码。
大家好,我在登录 google 时遇到问题另外,当我点击登录时没有任何反应。
我声明我导入了 API google,我在网站上打开了 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();
}
}
}
这是开始Activity
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.access0(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.access0(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)
它说您没有连接到 mGoogleApiClient。由于您未连接,因此也请添加 mGoogleApiClient.isConnected() 检查那里。
移植这些行
googlePlusLogin();
getProfileInformation();
到 onConnected 方法。
您必须在 google 控制台 "credentials" 部分注册您的应用程序。因此,也许您使用的是与添加到 google 控制台的密钥不同的密钥来签署您的应用程序。
您的问题是:
if (!mGoogleApiClient.isConnecting()) {
mSignInClicked = true;
resolveSignInError();
}
您当前的流程如下所示:
- 连接 GoogleAPIClient
- 如果 GoogleAPIClient 未连接,mSignInClicked = true
然后您将得到:onConnectionFailedCallback
您检查是否 mSignInClicked == true
如果是,请尝试解决错误。
将您的 googlePlusLogin 更改为
public void googlePlusLogin() {
mSignInClicked = true;
}
此错误的根本原因是您使用的代码对于用户交互式登录来说大部分都是正确的,并试图使其在 activity 创建时起作用。区别纯粹是时间。如果您的 googlePlusLogin 是由人类按下按钮(而不是 onStart)调用的,那么它可能没问题,因为 GoogleApiClient 可能已经完成了连接设置。通过在 activity 启动后立即执行此操作,您几乎可以保证它不会被连接,在这种情况下,您将坚持使用现有的代码。