【问题标题】:Google Plus Api Exception at runtime运行时的 Google Plus Api 异常
【发布时间】:2015-03-15 20:10:34
【问题描述】:

大家好,我在登录 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();
        }
    }
}

这是开始活动

            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)

【问题讨论】:

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


    【解决方案1】:

    你的问题是:

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

    您的当前流程如下所示:

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

    然后你会得到:onConnectionFailedCallback 在那里你检查mSignInClicked == true 如果是,尝试解决错误。

    将您的 googlePlusLogin 更改为

    public void googlePlusLogin() {
        mSignInClicked = true;
    }
    

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

    【讨论】:

    • 我检查了表单 onConnectionFailed ( ) ;放一个 Toast ' if (!mIntentInProgress) { mConnectionResult = result;获取配置文件信息(); Toast.makeText(getApplicationContext(), "测试 Toast", Toast.LENGTH_LONG).show(); }',当我按下登录时,我将返回 toast...
    • 作为测试,我会剥离你的代码 - 让它立即解决 onConnectionFailed 中的错误,看看你得到什么结果。
    • 任何东西,但我不明白为什么创建一个新项目并复制与登录相同的类是成功的,也许是用 Intent 调用类的事实?因此无法与 MainActivity 通信
    • 主要是因为您直接触发 AFAICS - 使用按钮加载类,等待,然后按下按钮,但在这里您基本上按下按钮作为活动加载过程的一部分。因为登录是一项后台活动,所以会有一点延迟,如果涉及到用户,这是不可见的,但如果它是作为加载过程的一部分发生的。
    • 更新了我的答案以反映。如果它有助于理解,请尝试绘制方法之间的流程。请记住 - .connect 调用不是即时的,它需要一些未知的时间。
    【解决方案2】:

    它说您没有连接到 mGoogleApiClient。由于您未连接,请添加 mGoogleApiClient.isConnected() 并检查那里。

    移植这些行
    googlePlusLogin(); getProfileInformation(); 到 onConnected 方法。

    您必须在 google 控制台“凭据”部分注册您的应用程序。因此,也许您使用不同的密钥来签署您的应用程序,然后是您添加到谷歌控制台的密钥。

    【讨论】:

    • 如果您仍然遇到与以前相同的错误,这意味着您在其他地方 onConnected 之前调用了 mGoogleApiClient。还是什么都没有发生?
    • 您是否使用生产密钥和 android 调试密钥添加了您的应用凭据?如此处所示Quick-start sample app for Android
    猜你喜欢
    • 2018-10-12
    • 1970-01-01
    • 2013-08-16
    • 2017-05-17
    • 1970-01-01
    • 1970-01-01
    • 2013-01-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多