在类W / ClassMapper上找不到字符串的setter或字段

时间:2018-01-07 04:06:15

标签: android firebase-realtime-database

我的用户模型类中定义了一个字符串birthdate,用于将birthdate的值从日期选择器对话框存储到共享的Preference和firebase数据库。这是我的用户模型类

 public class User {
  public String name;
  public String email;
  public String userName;
  public String dateOfBirth;
  public com.example.webczar.chat_navigate.Structure.Message message;
  public Status status;
  public String avata;

 public User() {

    message = new com.example.webczar.chat_navigate.Structure.Message();
    status = new Status();
    status.isOnline = false;
    status.timestamp = 0;
    message.idReceiver = "0";
    message.idSender = "0";
    message.text = "";
    message.timestamp = 0;
}

这是datepicker对话

 final Calendar calendar = Calendar.getInstance();
                int myear = calendar.get(Calendar.YEAR);
                int mmonth = calendar.get(Calendar.MONTH);
                int mdayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
                 datePicker = new DatePickerDialog(getActivity(),
                        new DatePickerDialog.OnDateSetListener() {
                            @Override
                            public void onDateSet(DatePicker view, 
       intyear, int month, int dayOfMonth) {

      String formatedDate = (dayOfMonth)+"/"+(month+1)+"/"+(year);
                                setPickedDate(formatedDate);
                            }
                        }, myear, mmonth, mdayOfMonth);
                    datePicker.show();

并且在设定的选择日期中,它将选择的日期存储在数据库和首选项中

   private void setPickedDate(String dob) { 
       userDB.child("birthDate").setValue(dob);
       user.dateOfBirth = dob;
       SharedPreferanceHelper preferanceHelper = 
       SharedPreferanceHelper.getInstance(context);
       preferanceHelper.saveUserInfo(user);
       Log.d(MainActivity.class.getSimpleName(),"not set dob");
       setUserValues(user); } 

即使它将值存储在firebase JSON树中,但它没有像在用户模型类中定义的其他变量一样显示文本视图中的值并抛出类映射器异常

谢谢。

1 个答案:

答案 0 :(得分:0)

当您使用以下代码行时:

userDB.child("birthDate").setValue(dob);

您正在数据库中创建新的子名birthDate。即使你在模型类中声明了这个字段,它也没有在构造函数中初始化,也没有公共setter和getter。为了解决这个问题,请以与其他方式相同的方式初始化该值。我重新命令你改变这样的模型类:

public class User {public String name;
    private String email;
    private String userName;
    private String dateOfBirth;
    private com.example.webczar.chat_navigate.Structure.Message message;
    private Status status;
    private String avata;

    public User() {
        //initialize fileds with desired values
    }

    public User(String name, String email, String userName, String dateOfBirth, com.example.webczar.chat_navigate.Structure.Message message, Status status, String avata) {
        this.name = name;
        this.email = email;
        this.userName = userName;
        this.dateOfBirth = dateOfBirth;
        this.message = message;
        this.status = status;
        this.avata = avata;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getDateOfBirth() {
        return dateOfBirth;
    }

    public void setDateOfBirth(String dateOfBirth) {
        this.dateOfBirth = dateOfBirth;
    }

    public com.example.webczar.chat_navigate.Structure.Message getMessage() {
        return message;
    }

    public void setMessage(com.example.webczar.chat_navigate.Structure.Message message) {
        this.message = message;
    }

    public Status getStatus() {
        return status;
    }

    public void setStatus(Status status) {
        this.status = status;
    }

    public String getAvata() {
        return avata;
    }

    public void setAvata(String avata) {
        this.avata = avata;
    }
}