如何将数据字段保存到 Firebase 用户输入中



我一直在尝试将用户输入的信息保存到Firebase,但我的应用程序已停止并崩溃.我创建了一个类,该类将采用我声明的变量的Getter方法,我试图更改如何保存值,但没有任何结果证明有效。主要活动.java

  }
    });
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            DoctorReg();
        }
    });
    }
  private void DoctorReg()
  {
      String name=text.getText().toString().trim();
      String lname=text1.getText().toString().trim();
      String sex=s.getSelectedItem().toString();
      String username=text2.getText().toString().trim();
      String password=text3.getText().toString().trim();
      String email=text4.getText().toString().trim();
      String pnum=text5.getText().toString().trim();
      String padd=text6.getText().toString().trim();
      String province=s1.getSelectedItem().toString();
      String speciality=s2.getSelectedItem().toString();
      if(!TextUtils.isEmpty(name))
      {
          String id=databaseReference.push().getKey();
          DoctorReg doctorReg= new DoctorReg(id,name,lname,sex,username,password,email,pnum,padd,province,speciality);
          databaseReference.child(id).setValue(doctorReg);
          Toast.makeText(this, "Patient is Added ",Toast.LENGTH_LONG).show();
      }
      else
      {
          Toast.makeText(this, "You should Enter a Name ",Toast.LENGTH_LONG).show();
      }
  }

文档注册.java

public final class DoctorReg {
String Doctor_Id,F_Name,L_Name,Sex,Username_Doc,Password_Doc,Email_Doc,PNum_Doc,Postal_Address_Doc,Province_Doc,Speciality;
public DoctorReg(){}
public DoctorReg(String doctor_Id, String f_Name, String l_Name, String sex, String username_Doc, String password_Doc, String email_Doc, String PNum_Doc, String postal_Address_Doc, String province_Doc, String speciality) {
    this.Doctor_Id = doctor_Id;
    this.F_Name = f_Name;
    this.L_Name = l_Name;
    this.Sex = sex;
    this.Username_Doc = username_Doc;
    this.Password_Doc = password_Doc;
    Email_Doc = email_Doc;
    this.PNum_Doc = PNum_Doc;
    this.Postal_Address_Doc = postal_Address_Doc;
    this.Province_Doc = province_Doc;
    this.Speciality = speciality;
}
public String getDoctor_Id() {
    return Doctor_Id;
}
public String getF_Name() {
    return F_Name;
}
public String getL_Name() {
    return L_Name;
}
public String getSex() {
    return Sex;
}
public String getUsername_Doc() {
    return Username_Doc;
}
public String getPassword_Doc() {
    return Password_Doc;
}
public String getEmail_Doc() {
    return Email_Doc;
}
public String getPNum_Doc() {
    return PNum_Doc;
}
public String getPostal_Address_Doc() {
    return Postal_Address_Doc;
}
public String getProvince_Doc() {
    return Province_Doc;
}
public String getSpeciality() {
    return Speciality;
}

日志猫

04-13 22:27:26.356 7323-7323/? E/AndroidRuntime: FATAL EXCEPTION: main
                                             Process: com.example.remy_stolz.myapplication, PID: 7323
                                             com.google.firebase.database.DatabaseException: Found two getters or fields with conflicting case sensitivity for property: doctor_id
                                                 at com.google.android.gms.internal.zh.zzhe(Unknown Source)
                                                 at com.google.android.gms.internal.zh.<init>(Unknown Source)
                                                 at com.google.android.gms.internal.zg.zzf(Unknown Source)
                                                 at com.google.android.gms.internal.zg.zzao(Unknown Source)
                                                 at com.google.android.gms.internal.zg.zzan(Unknown Source)
                                                 at com.google.firebase.database.DatabaseReference.zza(Unknown Source)
                                                 at com.google.firebase.database.DatabaseReference.setValue(Unknown Source)
                                                 at com.example.remy_stolz.myapplication.DocRegistrationActivity.DoctorReg(DocRegistrationActivity.java:120)
                                                 at com.example.remy_stolz.myapplication.DocRegistrationActivity$5.onClick(DocRegistrationActivity.java:99)
                                                 at android.view.View.performClick(View.java:5198)
                                                 at android.view.View$PerformClick.run(View.java:21147)
                                                 at android.os.Handler.handleCallback(Handler.java:739)
                                                 at android.os.Handler.dispatchMessage(Handler.java:95)
                                                 at android.os.Looper.loop(Looper.java:148)
                                                 at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

04-13 22:27:26

遵循Java的正确命名约定,这将通过解决冲突来帮助您解决此问题,并且还可以避免其他一些错误。也

  1. 对于课程,请使用UpperCamelCase.
  2. 对于类成员和局部变量,请使用lowerCamelCase
  3. 对于包,请使用反向 URI,例如 org.acme.project.subsystem
  4. 对于常量,请使用ALL_CAPS.

Firebase可能会发现getter方法getDoctor_Id()与成员变量Doctor_Id冲突。

因此,要么更改成员变量的大小写,使类如下所示,从而不再冲突:

public final class DoctorReg(){
    String doctor_Id;
    //The rest of your member variables here.
    public DoctorReg() {}
    public DoctorReg(String doctor_Id){
        this.doctor_Id = doctor_Id;
    }
    public String getDoctor_Id(){
        return doctor_Id;
    }
    //The rest of your getter methods below.
}

或者,尝试将成员变量设为私有Doctor_Id。由于未在原始类中指定访问修饰符,因此它默认为包私有。

相关内容

  • 没有找到相关文章

最新更新