ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

android-实施与Google的登录,并且变得奇怪“不幸的是,MyApp已停止”

2019-10-27 02:25:53  阅读:298  来源: 互联网

标签:google-signin android


我已经在我的应用中实现了Google登录按钮.

除了一件事,一切都运行良好,当意图提示用户选择要登录的Google帐户时,如果用户按“后退”按钮,不幸的是,MyApp将停止并且该应用将崩溃.

我真的不知道该从哪里开始选择解决方案,我们将不胜感激.

Hers是代码示例:

private SignInButton googleLogin;
private GoogleApiClient apiClient;
private GoogleSignInOptions gso;
private String googleUserName;
private String googleUserMail;
private static final int REQ_ID = 9001;

gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestEmail()
            .build();
    apiClient = new GoogleApiClient.Builder(this)
            .enableAutoManage(this,this)
            .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
            .build();

    googleLogin = (SignInButton) findViewById(R.id.googleSignIn);
    googleLogin.setSize(SignInButton.SIZE_STANDARD);
    googleLogin.setScopes(gso.getScopeArray());

googleLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent signIntent = Auth.GoogleSignInApi.getSignInIntent(apiClient);
                startActivityForResult(signIntent,REQ_ID);
            }
        });

@Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    callbackManager.onActivityResult(requestCode, resultCode, data);
    if(requestCode == REQ_ID)
    {
        GoogleSignInResult res = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
        GoogleSignInAccount account = res.getSignInAccount();
        googleUserName = account.getDisplayName().toString();
        googleUserMail = account.getEmail().toString();


    }
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {

}

这是日志:

`05-27 06:06:56.420 22660-22738/com.MyApp W/EGL_emulation: eglSurfaceAttrib not implemented
05-27 06:06:56.420 22660-22738/com.MyApp W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x99ada7c0, error=EGL_SUCCESS
05-27 06:07:03.147 22660-22660/com.MyApp D/AndroidRuntime: Shutting down VM
05-27 06:07:03.147 22660-22660/com.MyApp E/AndroidRuntime: FATAL EXCEPTION: main
                                                                       Process: com.MyApp, PID: 22660
                                                                       java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=9001, result=0, data=Intent { (has extras) }} to activity {com.MyApp/com.MyApp.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.android.gms.auth.api.signin.GoogleSignInAccount.getDisplayName()' on a null object reference
                                                                           at android.app.ActivityThread.deliverResults(ActivityThread.java:3699)
                                                                           at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742)
                                                                           at android.app.ActivityThread.-wrap16(ActivityThread.java)
                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393)
                                                                           at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                           at android.os.Looper.loop(Looper.java:148)
                                                                           at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                           at java.lang.reflect.Method.invoke(Native Method)
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                        Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.android.gms.auth.api.signin.GoogleSignInAccount.getDisplayName()' on a null object reference
                                                                           at com.door.global.store.MainActivity.onActivityResult(MainActivity.java:200)
                                                                           at android.app.Activity.dispatchActivityResult(Activity.java:6428)
                                                                           at android.app.ActivityThread.deliverResults(ActivityThread.java:3695)
                                                                           at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742) 
                                                                           at android.app.ActivityThread.-wrap16(ActivityThread.java) 
                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393) 
                                                                           at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                           at android.os.Looper.loop(Looper.java:148) 
                                                                           at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                           at java.lang.reflect.Method.invoke(Native Method) 
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
`

解决方法:

在onActivityResult中尝试一下

if(requestCode == REQ_ID && resultCode == Activity.RESULT_OK). 

因为它可能未登录,并且返回null.还要对帐户进行空检查.

GoogleSignInAccount account = res.getSignInAccount();
if(account != null){
        googleUserName = account.getDisplayName().toString();
        googleUserMail = account.getEmail().toString();
}

标签:google-signin,android
来源: https://codeday.me/bug/20191027/1941036.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有