3

I want to get the exceptions caused when a user fails to log into my app using Google Sign in. For example, in the handleSignInResult(@NonNull Task<GoogleSignInAccount> completedTask) method I use this:

private void handleSignInResult(@NonNull Task<GoogleSignInAccount> completedTask) { try { account = completedTask.getResult(ApiException.class); if(isSignedIn()) { Toast.makeText(this, "Success!", Toast.LENGTH_SHORT).show(); startActivity(new Intent(MainActivity.this, Home.class)); } } catch (ApiException e) { Toast.make(this, "Failed to login because: " + e.getCause(), Toast.LENGTH_LONG).show(); } } } 

This helps me to get the cause of the error. But the most common cause is that when when no account is selected, this code returns null.

But I want to make a Toast when no account is selcted:

Toast.makeText(this, "Failed to login because: No account selected!", Toast.LENGTH_LONG).show(); 

I think that this can be achieved using the switch case but I don't know how to do that.

3
  • 1
    if(e.getCause() != null){Toast.make(this, "Failed to login because: " + e.getCause(), Toast.LENGTH_LONG).show();}else{ Toast.make(this, "Failed to login because: No account selected!", Toast.LENGTH_LONG).show();} Commented Dec 13, 2018 at 17:44
  • Place that in your catch Commented Dec 13, 2018 at 17:45
  • Really hats off to you! Commented Dec 13, 2018 at 17:52

1 Answer 1

1

This is what I should use:

private void handleSignInResult(@NonNull Task<GoogleSignInAccount> completedTask) { try { account = completedTask.getResult(ApiException.class); if(isSignedIn()) { Toasty.success(this, "Success!", Toast.LENGTH_SHORT, true).show(); startActivity(new Intent(MainActivity.this, Home.class)); } } catch (ApiException e) { if(e.getCause() != null) { Toast.makeText(this, "Failed to login because: " + e.getCause(), Toast.LENGTH_LONG).show(); } else { Toast.makeText(this, "Failed to login because: No account Selected!", Toast.LENGTH_LONG).show(); } } }` 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.