'How to receive a name from firebase realtime database and show TextView

I want to receive name from firebase database.

After show TextView in Fragment, the run succeeded; however, the app turns off when O go to the Fragment where the TextView is located.

public class Frag3 extends Fragment {

    private FirebaseAuth mFirebaseAuth;
    private View view;
    private Button logout;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.frag3, container, false);
        mFirebaseAuth = FirebaseAuth.getInstance();

        TextView tv_showName = view.findViewById(R.id.tv_showName);

        FirebaseDatabase database = FirebaseDatabase.getInstance();
        DatabaseReference databaseUser = database.getReference("UserAccount").child("name");
        String id = mFirebaseAuth.getCurrentUser().getUid();
        DatabaseReference username = databaseUser.child(id).child("name");

        username.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                String username = snapshot.getValue().toString();
                tv_showName.setText(username);
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }
        });

        logout = view.findViewById(R.id.logout);
        logout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mFirebaseAuth.signOut();

                Intent intent = new Intent(getActivity(), LoginActivity.class);
                startActivity(intent);
            }
        });

        return view;
    }
}

This is my database:

database view



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source