Моя кнопка входа в Google не работает, и я не знаю почему.
Я создал OnClickListener, который должен работать, но кнопка входа в Google никогда не отвечает.
Вот мой код:
public class TabbedActivity extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
private static final String TAG = "LoginProcess";
Intent mapIntent;
SignInButton gsignInButton;
private static final int RC_SIGN_IN = 1;
LayoutInflater inflater;
View rootView;
FirebaseAuth mAuth;
FirebaseAuth.AuthStateListener mAuthListener;
/**
* The {@link ViewPager} that will host the section contents.
*/
private ViewPager mViewPager;
GoogleSignInOptions gso;
GoogleApiClient mGoogleApiClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tabbed);
mapIntent = new Intent(TabbedActivity.this, MapsActivity.class);
mAuth = FirebaseAuth.getInstance();
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
// User is signed in
Toast.makeText(TabbedActivity.this, "onAuthStateChanged:signed_in:" + user.getUid(), Toast.LENGTH_SHORT).show();
startActivity(mapIntent);
finish();
} else {
// User is signed out
Toast.makeText(TabbedActivity.this, "onAuthStateChanged:signed_out", Toast.LENGTH_SHORT).show();
}
}
};
inflater = getLayoutInflater();
rootView = inflater.inflate(R.layout.welcomescreenlogin, null);
gsignInButton = (SignInButton) rootView.findViewById(R.id.sign_in_button);
gsignInButton.setOnClickListener(onClick);
gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
View.OnClickListener onClick = new View.OnClickListener() {
public void onClick(View v) {
signIn();
}
};
@Override
protected void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
}
@Override
protected void onStop() {
super.onStop();
if (mAuthListener != null) {
mAuth.removeAuthStateListener(mAuthListener);
}
}
public void signIn() {
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, RC_SIGN_IN);
}
@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);
if (result.isSuccess()) {
// Google Sign In was successful, authenticate with Firebase
GoogleSignInAccount account = result.getSignInAccount();
firebaseAuthWithGoogle(account);
} else {
Toast.makeText(TabbedActivity.this, "Signup Error", Toast.LENGTH_LONG).show();
// Google Sign In failed, update UI appropriately
// ...
}
}
}
private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
Log.d(TAG, "firebaseAuthWithGoogle:" + acct.getId());
AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener() {
@Override
public void onComplete(@NonNull Task task) {
Log.d(TAG, "signInWithCredential:onComplete:" + task.isSuccessful());
// If sign in fails, display a message to the user. If sign in succeeds
// the auth state listener will be notified and logic to handle the
// signed in user can be handled in the listener.
if (!task.isSuccessful()) {
Log.w(TAG, "signInWithCredential", task.getException());
Toast.makeText(TabbedActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
}
// ...
}
});
}
}
Проблема может быть связана с тем, что класс расширяет «Appcompatactivity» или просто с тем, что другие методы для выполнения процесса входа написаны неправильно.
Моя кнопка входа в Google не работает, и я не знаю почему. Я создал OnClickListener, который должен работать, но кнопка входа в Google никогда не отвечает. Вот мой код: [code]public class TabbedActivity extends AppCompatActivity { private SectionsPagerAdapter mSectionsPagerAdapter; private static final String TAG = "LoginProcess"; Intent mapIntent; SignInButton gsignInButton; private static final int RC_SIGN_IN = 1; LayoutInflater inflater; View rootView; FirebaseAuth mAuth; FirebaseAuth.AuthStateListener mAuthListener;
/** * The {@link ViewPager} that will host the section contents. */ private ViewPager mViewPager; GoogleSignInOptions gso; GoogleApiClient mGoogleApiClient;
mAuthListener = new FirebaseAuth.AuthStateListener() { @Override public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) { FirebaseUser user = firebaseAuth.getCurrentUser(); if (user != null) { // User is signed in Toast.makeText(TabbedActivity.this, "onAuthStateChanged:signed_in:" + user.getUid(), Toast.LENGTH_SHORT).show(); startActivity(mapIntent); finish(); } else { // User is signed out Toast.makeText(TabbedActivity.this, "onAuthStateChanged:signed_out", Toast.LENGTH_SHORT).show();
gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) .requestIdToken(getString(R.string.default_web_client_id)) .requestEmail() .build();
mGoogleApiClient = new GoogleApiClient.Builder(this) .addApi(Auth.GOOGLE_SIGN_IN_API, gso) .build();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); // Create the adapter that will return a fragment for each of the three // primary sections of the activity. mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter. mViewPager = (ViewPager) findViewById(R.id.container); mViewPager.setAdapter(mSectionsPagerAdapter);
}
View.OnClickListener onClick = new View.OnClickListener() { public void onClick(View v) { signIn(); } };
@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); if (result.isSuccess()) { // Google Sign In was successful, authenticate with Firebase GoogleSignInAccount account = result.getSignInAccount(); firebaseAuthWithGoogle(account); } else { Toast.makeText(TabbedActivity.this, "Signup Error", Toast.LENGTH_LONG).show(); // Google Sign In failed, update UI appropriately // ... } } }
// If sign in fails, display a message to the user. If sign in succeeds // the auth state listener will be notified and logic to handle the // signed in user can be handled in the listener. if (!task.isSuccessful()) { Log.w(TAG, "signInWithCredential", task.getException()); Toast.makeText(TabbedActivity.this, "Authentication failed.", Toast.LENGTH_SHORT).show(); } // ... } }); } } [/code] А вот мой макет [code]
[/code] Проблема может быть связана с тем, что класс расширяет «Appcompatactivity» или просто с тем, что другие методы для выполнения процесса входа написаны неправильно.