我正在尝试从Firebase中提取数据并显示用户的名称和状态。我目前遇到空值异常。
public class ProfileFragment extends Fragment {
private DatabaseReference mUserDatabase;
private FirebaseUser mCurrentUser;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_profile, container, false);
CircleImageView displayProfilePicture = view.findViewById(R.id.settings_profile_picture);
final TextView mName = view.findViewById(R.id.settings_user_name);
final TextView mStatus = view.findViewById(R.id.settings_user_status);
mCurrentUser = FirebaseAuth.getInstance().getCurrentUser();
String current_uid = Objects.requireNonNull(mCurrentUser).getUid();
mUserDatabase = FirebaseDatabase.getInstance().getReference().child("Users").child(current_uid);
mUserDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
String name = (Objects.requireNonNull(dataSnapshot.child("name").getValue())).toString();
String status = Objects.requireNonNull(dataSnapshot.child("status").getValue()).toString();
String image = Objects.requireNonNull(dataSnapshot.child("image").getValue()).toString();
String thumb_image = Objects.requireNonNull(dataSnapshot.child("thumb image ").getValue()).toString();
mName.setText(name);
mStatus.setText(status);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
return view;
}}
我也尝试使用此代码以其他方式实现此目的,但我仍然无法显示数据。
public class ProfileFragment extends Fragment {
private DatabaseReference mUserDatabase;
private FirebaseUser mCurrentUser;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_profile, container, false);
CircleImageView displayProfilePicture = view.findViewById(R.id.settings_profile_picture);
final TextView mName = view.findViewById(R.id.settings_user_name);
final TextView mStatus = view.findViewById(R.id.settings_user_status);
mCurrentUser = FirebaseAuth.getInstance().getCurrentUser();
String current_uid = Objects.requireNonNull(mCurrentUser).getUid();
mUserDatabase = FirebaseDatabase.getInstance().getReference().child("Users").child(current_uid);
mUserDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot datas: dataSnapshot.getChildren()){
String keys=datas.getKey();
String displayname=datas.child("name").getValue().toString();
mName.setText(displayname);
}}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
return view;
}}
我的数据库结构如下:
答案 0 :(得分:1)
由于您已经拥有userId
,因此您无需循环即可访问直接子级。
尝试以下操作:
mUserDatabase = FirebaseDatabase.getInstance().getReference().child("Users").child(current_uid);
mUserDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
String keys=dataSnapshot.getKey();
String displayname=dataSnapshot.child("name").getValue().toString();
mName.setText(displayname);
}}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});