androidandroid-studiogoogle-signingoogle-plus-signin

Google Sign in with android Studio


I am doing sign in with Google account I have followed for creating a JSON file. Also I have integrate JSON file with my project.But I am getting exception that account.getDisplayName throws nullPointException.

enter image description here

Google sign in code:

public class MainActivity extends AppCompatActivity implements 
View.OnClickListener,GoogleApiClient.OnConnectionFailedListener {
private SignInButton signInButton;
private GoogleApiClient client;
private static final int REQ_CODE = 9001;

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

    signInButton = (SignInButton)findViewById(R.id.btn_signin);

    GoogleSignInOptions options = new 
 GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN)
            .requestProfile()
            .requestEmail()
            .build();
    client = new GoogleApiClient.Builder(this).
            enableAutoManage(this,this).
            addApi(Auth.GOOGLE_SIGN_IN_API,options).build();

    signInButton.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    switch (v.getId()){
        case R.id.btn_signin:
            signIn();
            break;
    }
}

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

}
    public void signIn(){
    Intent intent = Auth.GoogleSignInApi.getSignInIntent(client);
    startActivityForResult(intent,REQ_CODE);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable 
Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQ_CODE){
        GoogleSignInResult result = 
Auth.GoogleSignInApi.getSignInResultFromIntent(data);
        handleResult(result);
    }
}

 private void handleResult(GoogleSignInResult result) {
    if (result.isSuccess()){
        GoogleSignInAccount account = result.getSignInAccount();
        String name = account.getDisplayName();
            String email = account.getEmail();
            String image = account.getPhotoUrl().toString();
    }
}
}

and here is my compiled library:

implementation 'com.google.android.gms:play-services-auth:16.0.1'

please guide where I am doing mistake


Solution

  • Try this :

        @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 == REQ_CODE) {
                GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
                handleSignInResult(result);
    
                // G+
                Person person  = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
                System.out.println("Display Name: " + person.getDisplayName());
                System.out.println("Gender: " + person.getGender());
                System.out.println("AboutMe: " + person.getAboutMe());
                System.out.println("Birthday: " + person.getBirthday());
                System.out.println("Current Location: " + person.getCurrentLocation());
                System.out.println("Language: " + person.getLanguage());
    
            }
        }