2017-04-25 13 views
1

私は、この特定のチュートリアルhttp://www.androidhive.info/2014/02/android-login-with-google-plus-account-1/と私のアプリにGoogleログインを統合する方法に関する他のチュートリアルに従おうとしています。問題は私がアカウントを選んだときです。アプリが突然クラッシュする、誰でも私を助けてください。Googleログインアンドロイドクラッシュアプ​​リの統合

これは私が受け取るlogcatです:

E/AndroidRuntime: FATAL EXCEPTION: main 
       Process: com.example.gin.login, PID: 20197 
       java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=7, result=-1, data=Intent { (has extras) }} to activity {com.example.gin.login/com.example.gin.login.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.toString()' on a null object reference 
        at android.app.ActivityThread.deliverResults(ActivityThread.java:4734) 
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:4777) 
        at android.app.ActivityThread.access$1500(ActivityThread.java:211) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1754) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:145) 
        at android.app.ActivityThread.main(ActivityThread.java:6918) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:372) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199) 
       Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.toString()' on a null object reference 
        at com.example.gin.login.MainActivity.handleSignInResult(MainActivity.java:109) 
        at com.example.gin.login.MainActivity.onActivityResult(MainActivity.java:156) 
        at android.app.Activity.dispatchActivityResult(Activity.java:6833) 
        at android.app.ActivityThread.deliverResults(ActivityThread.java:4730) 
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:4777)  
        at android.app.ActivityThread.access$1500(ActivityThread.java:211)  
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1754)  
        at android.os.Handler.dispatchMessage(Handler.java:102)  
        at android.os.Looper.loop(Looper.java:145)  
        at android.app.ActivityThread.main(ActivityThread.java:6918)  
        at java.lang.reflect.Method.invoke(Native Method)  
        at java.lang.reflect.Method.invoke(Method.java:372)  
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)  
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)  

マイMainActivityクラス:

public class MainActivity extends AppCompatActivity implements View.OnClickListener, 
    GoogleApiClient.OnConnectionFailedListener { 

    private static final String TAG = MainActivity.class.getSimpleName(); 
    private static final int RC_SIGN_IN = 007; 

    private GoogleApiClient mGoogleApiClient; 
    private ProgressDialog mProgressDialog; 

    private SignInButton btnSignIn; 
    private Button btnSignOut, btnRevokeAccess; 
    private LinearLayout llProfileLayout; 
    private ImageView imgProfilePic; 
    private TextView txtName, txtEmail; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    btnSignIn = (SignInButton) findViewById(R.id.btn_sign_in); 
    btnSignOut = (Button) findViewById(R.id.btn_sign_out); 
    btnRevokeAccess = (Button) findViewById(R.id.btn_revoke_access); 
    llProfileLayout = (LinearLayout) findViewById(R.id.llProfile); 
    imgProfilePic = (ImageView) findViewById(R.id.imgProfilePic); 
    txtName = (TextView) findViewById(R.id.txtName); 
    txtEmail = (TextView) findViewById(R.id.txtEmail); 

    btnSignIn.setOnClickListener(this); 
    btnSignOut.setOnClickListener(this); 
    btnRevokeAccess.setOnClickListener(this); 

    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) 
      .requestEmail() 
      .build(); 

    mGoogleApiClient = new GoogleApiClient.Builder(this) 
      .enableAutoManage(this, this) 
      .addApi(Auth.GOOGLE_SIGN_IN_API, gso) 
      .build(); 

    // Customizing G+ button 
    btnSignIn.setSize(SignInButton.SIZE_STANDARD); 
    btnSignIn.setScopes(gso.getScopeArray()); 
} 

private void signIn() { 
    Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient); 
    startActivityForResult(signInIntent, RC_SIGN_IN); 
} 


private void signOut() { 
    Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(
      new ResultCallback<Status>() { 
       @Override 
       public void onResult(Status status) { 
        updateUI(false); 
       } 
      }); 
} 

private void revokeAccess() { 
    Auth.GoogleSignInApi.revokeAccess(mGoogleApiClient).setResultCallback(
      new ResultCallback<Status>() { 
       @Override 
       public void onResult(Status status) { 
        updateUI(false); 
       } 
      }); 
} 

private void handleSignInResult(GoogleSignInResult result) { 
    Log.d(TAG, "handleSignInResult:" + result.isSuccess()); 
    if (result.isSuccess()) { 
     // Signed in successfully, show authenticated UI. 
     GoogleSignInAccount acct = result.getSignInAccount(); 

     Log.e(TAG, "display name: " + acct.getDisplayName()); 

     String personName = acct.getDisplayName(); 
     String personPhotoUrl = acct.getPhotoUrl().toString(); 
     String email = acct.getEmail(); 

     Log.e(TAG, "Name: " + personName + ", email: " + email 
       + ", Image: " + personPhotoUrl); 

     txtName.setText(personName); 
     txtEmail.setText(email); 
     Glide.with(getApplicationContext()).load(personPhotoUrl) 
       .thumbnail(0.5f) 
       .crossFade() 
       .diskCacheStrategy(DiskCacheStrategy.ALL) 
       .into(imgProfilePic); 

     updateUI(true); 
    } else { 
     // Signed out, show unauthenticated UI. 
     updateUI(false); 
    } 
} 

@Override 
public void onClick(View v) { 
    int id = v.getId(); 

    switch (id) { 
     case R.id.btn_sign_in: 
      signIn(); 
      break; 

     case R.id.btn_sign_out: 
      signOut(); 
      break; 

     case R.id.btn_revoke_access: 
      revokeAccess(); 
      break; 
    } 
} 

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 

    // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...); 
    if (requestCode == RC_SIGN_IN) { 
     GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data); 
     handleSignInResult(result); 
    } 
} 

@Override 
public void onStart() { 
    super.onStart(); 

    OptionalPendingResult<GoogleSignInResult> opr = Auth.GoogleSignInApi.silentSignIn(mGoogleApiClient); 
    if (opr.isDone()) { 
     // If the user's cached credentials are valid, the OptionalPendingResult will be "done" 
     // and the GoogleSignInResult will be available instantly. 
     Log.d(TAG, "Got cached sign-in"); 
     GoogleSignInResult result = opr.get(); 
     handleSignInResult(result); 
    } else { 
     // If the user has not previously signed in on this device or the sign-in has expired, 
     // this asynchronous branch will attempt to sign in the user silently. Cross-device 
     // single sign-on will occur in this branch. 
     showProgressDialog(); 
     opr.setResultCallback(new ResultCallback<GoogleSignInResult>() { 
      @Override 
      public void onResult(GoogleSignInResult googleSignInResult) { 
       hideProgressDialog(); 
       handleSignInResult(googleSignInResult); 
      } 
     }); 
    } 
} 

@Override 
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { 
    // An unresolvable error has occurred and Google APIs (including Sign-In) will not 
    // be available. 
    Log.d(TAG, "onConnectionFailed:" + connectionResult); 
} 

private void showProgressDialog() { 
    if (mProgressDialog == null) { 
     mProgressDialog = new ProgressDialog(this); 
     mProgressDialog.setMessage("LOADING"); 
     mProgressDialog.setIndeterminate(true); 
    } 

    mProgressDialog.show(); 
} 

private void hideProgressDialog() { 
    if (mProgressDialog != null && mProgressDialog.isShowing()) { 
     mProgressDialog.hide(); 
    } 
} 

private void updateUI(boolean isSignedIn) { 
    if (isSignedIn) { 
     btnSignIn.setVisibility(View.GONE); 
     btnSignOut.setVisibility(View.VISIBLE); 
     btnRevokeAccess.setVisibility(View.VISIBLE); 
     llProfileLayout.setVisibility(View.VISIBLE); 
    } else { 
     btnSignIn.setVisibility(View.VISIBLE); 
     btnSignOut.setVisibility(View.GONE); 
     btnRevokeAccess.setVisibility(View.GONE); 
     llProfileLayout.setVisibility(View.GONE); 
    } 
} 

これは私のグレード(プロジェクト)である:

dependencies { 
    classpath 'com.android.tools.build:gradle:2.2.3' 

    classpath 'com.google.gms:google-services:3.0.0' 
} 

これは私のグレード(モジュールです):

dependencies { 
    compile 'com.google.android.gms:play-services-auth:9.2.1' 

compile 'com.github.bumptech.glide:glide:3.7.0' 
} 

事前にお問い合わせいただきありがとうございます。 :D

+0

私は私のコードの先生を追加した。また –

+0

@MMあなたのアクティビティコードを追加します:D –

+0

あなたは、Googleのサインインを有効にしましたか? –

答えて

1

 String personPhotoUrl = acct.getPhotoUrl().toString(); 

のように見えるがエラーを投げています。

 String photoUrl = acct.getPhotoUrl(); 
     String personPhotoUrl; 
     if (photoUrl != null) { 
      personPhotoUrl = photoUrl.toString(); 
     } 
+0

に追加しました。あなたは正しいです。本当にありがとう、本当にありがとう! –

+0

私の喜び良いサー! –

1

ユーザーが写真を持っていないので、nullであるか、ない場合はUriをチェックする必要があります。getPhotoUrl()がnullまたはしない)(のtoStringを呼び出そうとする前にあるかどうかを確認してください。

String photoUrl = acct.getPhotoUrl() != null ? acct.getPhotoUrl().toString : "";

+0

はい、あなたは正しいです。本当にありがとう、本当にありがとう! あなたはどちらも正しいので、私は1つの答えしか受け入れることができませんでした。もう一度おねがいします! –

+0

投票できます。喜んで。 –