I'm trying to implement Google Sign-In in my Android app using CredentialManager. However, I'm encountering an issue where the selector UI fails to launch, and I receive the error message: "failed to launch the selector UI. Hint: ensure the context parameter is an Activity-based context."
Here's the relevant code snippet:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.clickme);
button.setOnClickListener(v -> {
Context context = getApplicationContext();
CredentialManager credentialManager = CredentialManager.create(context);
GetGoogleIdOption googleIdOption = new GetGoogleIdOption.Builder()
.setFilterByAuthorizedAccounts(true)
.setServerClientId("clientID")
.build();
GetCredentialRequest request = new GetCredentialRequest.Builder()
.addCredentialOption(googleIdOption)
.build();
credentialManager.getCredentialAsync(context, request, new CancellationSignal(),
Executors.newSingleThreadExecutor(),
new CredentialManagerCallback<GetCredentialResponse, GetCredentialException>() {
@Override
public void onResult(GetCredentialResponse getCredentialResponse) {
Log.d("onresult", "Happened");
}
@Override
public void onError(@NonNull GetCredentialException e) {
Log.d("onerror", Objects.requireNonNull(e.getMessage()));
}
});
});
}
}
I’ve verified that the client ID is correct, but I’m still encountering this issue. How can I resolve it and successfully launch the Google Sign-In selector UI? Any insights or suggestions would be greatly appreciated!
I attempted to implement Google Sign-In using CredentialManager in my Android app in java.However, when I click the associated button, I encounter the following error message: “failed to launch the selector UI. Hint: ensure the context parameter is an Activity-based context.”
I expected the Google Sign-In selector UI to launch successfully, allowing users to sign in with their Google accounts.
You have:
Context context = getApplicationContext();
CredentialManager credentialManager = CredentialManager.create(context);
The "context" that you're using is the "Application" context and as the error states, it needs an Activity context, so correct that and see if it helps or not. Also, after making that change, you first might want to set setFilterByAuthorizedAccounts(false) during your initial test to make sure the UI shows up and then change that back to true and then false if you get the No Credential errors.
I changed "Context context = getApplicationContext" to "Context context = this" and now it works.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With