java - Google Plus Api 运行时异常

标签 java android android-activity google-plus google-play-services

大家好,我在登录 google plus 时遇到问题,当我点击登录时没有任何反应。

我声明我导入了 google 的 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();
        }
    }
}

这是启动 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.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)

最佳答案

您的问题是:

if (!mGoogleApiClient.isConnecting()) {           
    mSignInClicked = true;
    resolveSignInError();
}

您当前的流程如下所示:

  • 连接 GoogleAPIClient
  • 如果 GoogleAPIClient 未连接,则 mSignInClicked = true

然后您将得到:onConnectionFailedCallback,您可以在其中检查 mSignInClicked == true 如果是,请尝试解决错误。

将您的 googlePlusLogin 更改为

public void googlePlusLogin() {
    mSignInClicked = true;
}

此错误的根本原因是您使用的代码对于用户交互式登录来说基本上是正确的,并尝试使其在 Activity 创建时工作。区别纯粹是时间。如果您的 googlePlusLogin 是由人按下按钮(而不是 onStart)调用的,则可能没问题,因为 GoogleApiClient 可能已完成连接设置。通过在 Activity 开始后立即执行此操作,您几乎可以保证它不会被连接,在这种情况下,您将继续使用现有的代码。

关于java - Google Plus Api 运行时异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29065635/

相关文章:

java - 如何在android中的azure离线同步中异步同步多个表

android - 如何通过 GPRS 或 3G 连接两个 Android 设备?

java - 在图表上放置正确的刻度标记

java - 适用于 Android 的 Adob​​e AIR 打包

Android:倾斜移动使 ImageView 内容消失/URI 刷新

android - Main Activity 销毁后不会被垃圾回收,因为它被 InputMethodManager 间接引用

android - 在 Android 上显示调试横幅

java - 迭代添加 ArrayDeque 中的所有元素

java - Clover 和 Jacoco 给出不同的代码覆盖率结果

android - 我什么时候需要基础 Activity 和基础 fragment ?