我有一个登录活动,在这个活动中,用户可以通过firebase登录他们的Google账户,它工作得很完美,但我遇到的问题是将这些信息发送到导航抽屉。我在StackOverflow上几乎尝试了所有关于这个问题的代码,但似乎没有任何东西能与我的代码一起工作,有人能帮助我吗,我非常感激。
这是我目前所尝试的。
MainActivity
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private DrawerLayout drawer;
TextView text;
ImageView image;
FirebaseAuth mAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
text = findViewById(R.id.nav_text);
image = findViewById(R.id.nav_image);
mAuth = FirebaseAuth.getInstance();
if (mAuth.getCurrentUser() != null) {
FirebaseUser user = mAuth.getCurrentUser();
updateUI(user);
}
drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
View header = navigationView.getHeaderView(0);
TextView text = (TextView) header.findViewById(R.id.nav_text);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar,
R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
}
private void updateUI(FirebaseUser user) {
if (user != null) {
String name = user.getDisplayName();
String email = user.getEmail();
String photo = String.valueOf(user.getPhotoUrl());
text.append(name + "\n");
text.append(email);
Picasso.get().load(photo).into(image);
} else {
Picasso.get().load(R.drawable.ic_firebase_logo).into(image);
}
}
导航头.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="176dp"
android:background="@color/colorPrimary"
android:gravity="bottom"
android:orientation="vertical"
android:padding="16dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark">
<ImageView
android:id="@+id/nav_image"
android:layout_width="70dp"
android:layout_height="70dp"
android:src="@drawable/ic_firebase_logo"
android:contentDescription="@string/app_name" />
<TextView
android:id="@+id/nav_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="8dp"
android:text="@string/user_1234"
android:textAppearance="@style/TextAppearance.AppCompat.Body1" />
<TextView
android:id="@+id/nav_mail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/info_user1234_com" />
登录活动
public class LoginActivity extends AppCompatActivity {
static final int GOOGLE_SIGN_IN = 123;
FirebaseAuth mAuth;
Button btn_login, btn_logout;
TextView text;
ImageView image;
ProgressBar progressBar;
GoogleSignInClient mGoogleSignInClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
btn_login = findViewById(R.id.login);
btn_logout = findViewById(R.id.logout);
text = findViewById(R.id.text);
image = findViewById(R.id.image);
progressBar = findViewById(R.id.progress_circular);
mAuth = FirebaseAuth.getInstance();
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build();
mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
btn_login.setOnClickListener(v -> SignInGoogle());
btn_logout.setOnClickListener(v -> Logout());
if (mAuth.getCurrentUser() != null) {
FirebaseUser user = mAuth.getCurrentUser();
updateUI(user);
}
}
public void SignInGoogle() {
progressBar.setVisibility(View.VISIBLE);
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, GOOGLE_SIGN_IN);
}
private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
Log.d("TAG", "firebaseAuthWithGoogle:" + acct.getId());
AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, task -> {
if (task.isSuccessful()) {
progressBar.setVisibility(View.INVISIBLE);
Log.d("TAG", "signInWithCredential:success");
FirebaseUser user = mAuth.getCurrentUser();
updateUI(user);
} else {
progressBar.setVisibility(View.INVISIBLE);
Log.w("TAG", "signInWithCredential:failure", task.getException());
Toast.makeText(LoginActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
updateUI(null);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == GOOGLE_SIGN_IN) {
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
try {
GoogleSignInAccount account = task.getResult(ApiException.class);
if (account != null) firebaseAuthWithGoogle(account);
} catch (ApiException e) {
Log.w("TAG", "Google sign in failed", e);
}
}
}
private void updateUI(FirebaseUser user) {
if (user != null) {
String name = user.getDisplayName();
String email = user.getEmail();
String photo = String.valueOf(user.getPhotoUrl());
text.append("Info : \n");
text.append(name + "\n");
text.append(email);
Picasso.get().load(photo).into(image);
btn_logout.setVisibility(View.VISIBLE);
btn_login.setVisibility(View.INVISIBLE);
} else {
text.setText("Firebase Login \n");
Picasso.get().load(R.drawable.ic_firebase_logo).into(image);
btn_logout.setVisibility(View.INVISIBLE);
btn_login.setVisibility(View.VISIBLE);
}
}
private void Logout() {
FirebaseAuth.getInstance().signOut();
mGoogleSignInClient.signOut().addOnCompleteListener(this,
task -> updateUI(null));
}
activity_login.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimaryDark"
tools:context="com.sciyoutec.sci_youtec.LoginActivity">
<ImageView
android:id="@+id/image"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerHorizontal="true"
android:scaleType="centerCrop"
android:layout_marginTop="60dp"
android:src="@drawable/ic_firebase_logo"
android:contentDescription="@string/app_name"/>
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/image"
android:layout_marginTop="20dp"
android:gravity="center"
android:text="@string/google_login"
android:textColor="@color/action"
android:textSize="16sp" />
<ProgressBar
android:visibility="invisible"
android:id="@+id/progress_circular"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/text"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"/>
<Button
android:id="@+id/login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="60dp"
android:layout_marginLeft="26dp"
android:layout_marginRight="26dp"
android:text="@string/login_with_google"
android:background="@drawable/round_bg"
android:drawableStart="@drawable/ic_google_logo"
android:drawableLeft="@drawable/ic_google_logo"/>
<Button
android:visibility="invisible"
android:id="@+id/logout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="60dp"
android:layout_marginLeft="26dp"
android:layout_marginRight="26dp"
android:text="@string/logout"
android:background="@drawable/round_bg"/>
Logcat崩溃报告
05-19 10:36:09.734 5132-22581? EPairHttpConnection.S3请求>16K,可能失败(size=16868)。S3请求>16K,可能失败(size=16868) http:/b15866117
有同样的问题。做了这个。
NavigationView navigationView = findViewById(R.id.nav_view);
View headerLayout = navigationView.getHeaderView(0);
.
.
.
text = headerLayout.findViewById(R.id.nav_text);
image = headerLayout.findViewById(R.id.nav_image);
我不知道你是否尝试过这个,但它为我工作。