2016-05-27 8 views
3

Ich habe Google Sign-In-Taste in meiner App implementiert.Implementieren Anmeldung mit Google, und seltsam geworden "leider hat MyApp gestoppt"

Google Sign in Button image

Alles funktioniert sehr gut bis auf eine Sache, wenn es die Absicht, den Benutzer auffordert, ein Google-Konto auswählen, mit anzumelden, wenn der Benutzer drücken Sie die Zurück-Taste er unfortunately, MyApp has stopped und die App abstürzt bekommen.

Ich weiß wirklich nicht, wo ich anfangen sollte, eine Lösung für dieses Problem auszuwählen, jede Hilfe würde geschätzt werden.

Hers ist ein Beispiel für den Code:

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) { 

} 

Und hier ist das Protokoll:

`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)  
` 
+0

Können Sie die Protokolle veröffentlichen? – Raghavendra

+0

sicher, ich bin auf ... wie bald krank wieder nach Hause krank Post – 2Stoned

+0

Ich hoffe, das Protokoll ist lesbar, wenn nicht, sag mir, was zu tun ist und es zu beheben – 2Stoned

Antwort

3

Versuchen Sie dies in onActivityResult

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

Weil es nicht in wahrscheinlich unterzeichnet und es gibt null zurück. Machen Sie auch einen Null-Check für ein Konto.

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

Akzeptiert :) Raghavendra – 2Stoned

+0

bitte Refrain Formular mit solchen Worten @ 2Stoned –