In my app, I created login and signup page and saved the user name and email to my Firebase database. I also made bottom navigation with fragment. I want to take these values(name, mail) from Firebase and put it into TextView in profile fragment. When I tried this, my app crashed. How can I solve this problem?
public class ProfileFragment extends Fragment {
private FirebaseDatabase mFirebaseDatabase;
private FirebaseAuth mAuth;
private String userID;
private TextView tName;
private TextView tEmail;
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View profileView = inflater.inflate(R.layout.fragment_profile, null);
tName=profileView.findViewById(R.id.tv_name);
tEmail=profileView.findViewById(R.id.tv_email);
mAuth=FirebaseAuth.getInstance();
mFirebaseDatabase=FirebaseDatabase.getInstance();
final DatabaseReference myRef=mFirebaseDatabase.getReference("users");
FirebaseUser user=mAuth.getCurrentUser();
userID=user.getUid();
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds: dataSnapshot.getChildren()){
User user=new User();
user.setUserName(ds.child(userID).getValue(User.class).getUserName());
user.setUserEmail(ds.child(userID).getValue(User.class).getUserEmail());
tName.setText(user.getUserName());
tEmail.setText(user.getUserEmail());
}
}
@Override
public void onCancelled(DatabaseError databaseError) {}
});
return profileView;
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…