无法在firebase数据库中设置文本查看器上的文本

时间:2017-07-14 11:57:45

标签: java android firebase firebase-realtime-database firebase-authentication

首先,我是新手

我正在尝试使用firebase设计登录/注册系统。一切都运行良好,但每当我试图在TextView上显示名称和地址时,虽然所有信息都在数据库上更新,但它没有显示任何内容。

这是我的 Profile.java

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_profile);

    textViewUserEmail = (TextView) findViewById(R.id.textViewUserEmail);
    textViewFullName = (TextView) findViewById(R.id.textViewFullName);
    textViewAddress = (TextView) findViewById(R.id.textViewAddress);
    editTextFullName = (EditText) findViewById(R.id.editTextFullName);
    editTextAddress = (EditText) findViewById(R.id.editTextAddress);
    buttonSaveInfo = (Button) findViewById(R.id.buttonSaveInfo);
    buttonLogOut = (Button) findViewById(R.id.buttonLogOut);

    buttonSaveInfo.setOnClickListener(this);
    buttonLogOut.setOnClickListener(this);

    firebaseAuth = FirebaseAuth.getInstance();
    if (firebaseAuth.getCurrentUser() == null){
        finish();
        startActivity(new Intent(getApplicationContext(), Login.class));
    }

    FirebaseUser user = firebaseAuth.getCurrentUser(); //to use the data of the current user
    textViewUserEmail.setText("Welcome "+user.getEmail());

    databaseReference = FirebaseDatabase.getInstance().getReference();//needed to view or input anything from/to database




////////////////Here is my setText code:////////////////////////////

    if (firebaseAuth.getCurrentUser() != null){
        UserInformation uInfo = new UserInformation();

        //display all the information
        Log.d(TAG, "showData: name: " + uInfo.getName());
        Log.d(TAG, "showData: address: " + uInfo.getAddress());

        textViewFullName.setText(uInfo.getName());
        textViewAddress.setText(uInfo.getAddress());
    }
////////////////////////////////////////////////////
}

@Override
public void onClick(View v) {
    if (v == buttonLogOut){
        firebaseAuth.signOut();
        finish();
        startActivity(new Intent(this, Login.class));
    }
    if (v == buttonSaveInfo){
        saveUserInformation();
    }
}

private void saveUserInformation(){
    String name = editTextFullName.getText().toString().trim();
    String address = editTextAddress.getText().toString().trim();

    UserInformation userInformation = new UserInformation(name, address);

    //to store data to the individual profiles, I will use the unique id(s) of the logged in firebase user profiles.
    FirebaseUser user = firebaseAuth.getCurrentUser();

    databaseReference.child(user.getUid()).setValue(userInformation);

    Toast.makeText(this, "Profile Updated", Toast.LENGTH_SHORT).show();
}
}

这是我的 UserInformation.java

public class UserInformation {

public String name;
public String address;

public UserInformation(){

}

public UserInformation(String name, String address){
    this.name = name;
    this.address = address;
}

public String getName(){
    return name;
}

public String getAddress(){
    return address;
}
}

1 个答案:

答案 0 :(得分:1)

使用其空构造函数创建新的UserInformation对象。因此,nameaddress为空,因此当您调用getName()getAddress()时,它将返回null并打印出一个空值。

使用此构造函数初始化nameaddress值:

public UserInformation(String name, String address)