0

Im currently trying to implement Google Sign in into my app, when i pressed sign in the google intend opened and i could select a google account but now it crashes everytime I press the button... I tried the solutions i found on the internet but they didnt help me.. I have the following errors when pressing "sign in":

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.name, PID: 14299
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=9001, result=-1, data=Intent { (has extras) }} to activity {com.name/com.name.LoginActivity}: 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:4053)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4096)
at android.app.ActivityThread.-wrap20(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1516)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
 Causeed by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.toString()' on a null object reference
at com.name.LoginActivity.handleSignInResult(LoginActivity.java:125)
at com.name.LoginActivity.onActivityResult(LoginActivity.java:112)
at android.app.Activity.dispatchActivityResult(Activity.java:6915)
at android.app.ActivityThread.deliverResults(ActivityThread.java:4049)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4096) 
at android.app.ActivityThread.-wrap20(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1516) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6077) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) 

My methods are:

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

private static final String TAG = "SignInActivity";
private static final int RC_SIGN_IN = 9001;

private TextView mStatusTextView;
private ProgressDialog mProgressDialog;

private TextView Name;
private TextView Email;
private ImageView ProfilePic;
private RelativeLayout Account_layout;
private SignInButton signInButton;
private Button signOutButton;
private GoogleApiClient mGoogleApiClient;

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

    //findViewById(R.id.sign_in_button).setOnClickListener((View.OnClickListener) this);


    final PacketBuilder pb = new PacketBuilder();
    final Context context = this;
    signInButton = (SignInButton) findViewById(R.id.sign_in_button);
    signInButton.setSize(SignInButton.SIZE_STANDARD);
    signOutButton = (Button) findViewById(R.id.sign_out_button);
    Name = (TextView) findViewById(R.id.account_name);
    Email = (TextView) findViewById(R.id.account_email);
    ProfilePic = (ImageView) findViewById(R.id.account_picture);
    Account_layout = (RelativeLayout) findViewById(R.id.account_layout);
    Account_layout.setVisibility(View.GONE);

    signInButton.setOnClickListener(this);
    signOutButton.setOnClickListener(this);


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

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


}

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

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(@NonNull Status status) {
            updateUI(false);
        }
    });
}

@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);
        }

}

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

        String name = acc.getDisplayName();
        String email = acc.getEmail();
        String picture_url = acc.getPhotoUrl().toString();
        Name.setText(name);
        Email.setText(email);
        Glide.with(this).load(picture_url).into(ProfilePic);
        updateUI(true);

        //mStatusTextView.setText(getString(R.string.signed_in_fmt, acc.getDisplayName()));

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

private void updateUI(boolean signedIn) {
    if (signedIn) {
        findViewById(R.id.sign_in_button).setVisibility(View.GONE);
        findViewById(R.id.account_layout).setVisibility(View.VISIBLE);
    } else {
        //mStatusTextView.setText(R.string.signed_out);

        findViewById(R.id.sign_in_button).setVisibility(View.VISIBLE);
        findViewById(R.id.account_layout).setVisibility(View.GONE);
    }
}

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

}

I can't find the problem :( Hope someone can help me.

Boken
  • 4,825
  • 10
  • 32
  • 42

1 Answers1

0

Replace acc.getPhotoUrl().toString() with acc.getPhotoUrl() != null ? user.getPhotoUrl().toString() : null cause problem is fetching picture from url.

Remember :

getPhotoUrl () Gets the photo url of the signed in user.

It returns the photo url for the Google account. Only non-null if requestProfile() is configured and user does have a Google+ profile picture. Please check if your Google account has had Google+ profile picture or not.

P/S: sometimes, if Google+ profile picture has been created already but after the time you add Google account in your device, perhaps you need to delete that existing Google account from your device, then re-add.

Hope this might work.

Sahdeep Singh
  • 1,342
  • 1
  • 13
  • 34