有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

首先,我是一个新手

我正在尝试使用firebase设计一个登录/注册系统。一切都很好,但每当我试图在TextView上显示姓名和地址时,它都不会显示任何内容,尽管数据库上的所有信息都在更新

这是我的个人资料。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();
}
}

这是我的用户信息。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) 个答案

  1. # 1 楼答案

    使用空构造函数创建一个新的UserInformation对象。因此,它的nameaddress是空的,所以当调用getName()getAddress()时,它返回空值并输出空值

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

    public UserInformation(String name, String address)