googlesigninbutton 对点击没有反应
googlesigninbutton doesnt react to click
我的 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<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> 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();
}
// ...
}
});
}
}
这是我的布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/welcomegradientlogin">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Sign in to Google"
android:id="@+id/Welcome"
android:layout_gravity="center_horizontal|center_vertical"
android:textColor="#FFFF"
android:textSize="35sp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<com.google.android.gms.common.SignInButton
android:id="@+id/sign_in_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/Welcome"
android:layout_centerHorizontal="true"
android:layout_marginTop="24dp"
/>
</RelativeLayout>
问题可能与 class 扩展 "Appcompatactivity" 或其他执行登录过程的方法编写不正确有关。
please add this line in Button
android:onClick="onClick"
<com.google.android.gms.common.SignInButton
android:id="@+id/sign_in_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/Welcome"
android:layout_centerHorizontal="true"
android:layout_marginTop="24dp"
/>
我的 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<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> 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();
}
// ...
}
});
}
}
这是我的布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/welcomegradientlogin">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Sign in to Google"
android:id="@+id/Welcome"
android:layout_gravity="center_horizontal|center_vertical"
android:textColor="#FFFF"
android:textSize="35sp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<com.google.android.gms.common.SignInButton
android:id="@+id/sign_in_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/Welcome"
android:layout_centerHorizontal="true"
android:layout_marginTop="24dp"
/>
</RelativeLayout>
问题可能与 class 扩展 "Appcompatactivity" 或其他执行登录过程的方法编写不正确有关。
please add this line in Button
android:onClick="onClick"
<com.google.android.gms.common.SignInButton
android:id="@+id/sign_in_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/Welcome"
android:layout_centerHorizontal="true"
android:layout_marginTop="24dp"
/>