0

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;
    }
}
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • we need to see your database structure :) – Levi Moreira Apr 27 '18 at 14:12
  • also post your error ! – iamkdblue Apr 27 '18 at 14:14
  • ı cant have enough rep to upload image. I can explain easily , ın my firebase database , ı have users reference, in the user reference , ı have several child names user id , and the child values name email – Burak İNAL Apr 27 '18 at 14:25
  • FATAL EXCEPTION: main Process: com.example.burak.anonstezapp, PID: 22212 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.example.burak.anonstezapp.User.getUserName()' on a null object reference – Burak İNAL Apr 27 '18 at 14:30

2 Answers2

0

create file in xml folder

<?xml version="1.0" encoding="utf-8"?>
<defaultsMap>
    <entry>
       <key>email</key>
       <value>name</value>
</entry>

public FirebaseRemoteConfig getmFirebaseRemoteConfig() {

    mFirebaseRemoteConfig = FirebaseRemoteConfig.getInstance();

    FirebaseRemoteConfigSettings configSettings = new FirebaseRemoteConfigSettings.Builder()
             //.setDeveloperModeEnabled(BuildConfig.DEBUG)
            .setDeveloperModeEnabled(false)
            .build();
    mFirebaseRemoteConfig.setConfigSettings(configSettings);

    mFirebaseRemoteConfig.setDefaults(R.xml.remote_config_defaults);


    return mFirebaseRemoteConfig;
}
Hardik Vasani
  • 876
  • 1
  • 8
  • 14
0

Assuming that your database structure looks similar to this:

Firebase-root
   |
   --- users
         |
         --- uid
              |
              --- name: "John"
              |
              --- email: "john@email.com"

To print the name and the email, please use the following code using a model class:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef = rootRef.child("users").child(uid);
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        User user = dataSnapshot.getValue(User.class);
        String name = user.getUserName();
        tName.setText(name);
        String email = user.getUserEmail();
        tEmail.setText(email);
        Log.d("TAG", name + " / " + email);
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
uidRef.addListenerForSingleValueEvent(valueEventListener);

If you want to print them even simpler using the String class, please use the following code:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference usersRef = rootRef.child("users").child(uid);
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        String name = dataSnapshot.child("name").getValue(String.class);
        tName.setText(name);
        String email = dataSnapshot.child("email").getValue(String.class);
        tEmail.setText(email);
        Log.d("TAG", name + " / " + email);
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
uidRef.addListenerForSingleValueEvent(valueEventListener);

In both cases, the output will be: John / john@email.com

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193