我想向我展示cardview中从firebase databse中的所有用户,这些用户处于recyclerview中,但在调试器中,向我显示UserActivity中第60行的错误。 我在数据库中放了一个屏幕截图:
UsersFragment:
public class UsersFragment extends Fragment {
private RecyclerView recyclerView;
private UserAdapter userAdapter;
private List<User> mUsers;
@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view= inflater.inflate ( R.layout.fragment_users3, container, false );
recyclerView=view.findViewById (R.id.recycler_view );
recyclerView.setLayoutManager ( new LinearLayoutManager ( getContext () ) );
mUsers=new ArrayList<> ( );
readUsers();
return view;
}
private void readUsers () {
final FirebaseUser firebaseUser= FirebaseAuth.getInstance ().getCurrentUser ();
DatabaseReference referenc= FirebaseDatabase.getInstance ().getReference ("users");
referenc.addValueEventListener ( new ValueEventListener () {
@Override
public void onDataChange (@NonNull DataSnapshot dataSnapshot) {
mUsers.clear ();
for(DataSnapshot snapshot:dataSnapshot.getChildren ()){
User user=snapshot.getValue (User.class);
assert user!=null;
assert firebaseUser!=null;
if(!firebaseUser.getUid ().equals ( user.getUid () )){
mUsers.add ( user );
}
}
userAdapter=new UserAdapter ( getContext (),mUsers );
recyclerView.setAdapter ( userAdapter );
}
@Override
public void onCancelled (@NonNull DatabaseError databaseError) {
}
} );
}
}
UserAdapter。在我中使用了User类:
public class UserAdapter extends RecyclerView.Adapter<UserAdapter.ViewHolder> {
private Context mContext;
private List<User> mUsers;
public UserAdapter(Context mContext,List<User> mUsers){
this.mUsers=mUsers;
this.mContext=mContext;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder (@NonNull ViewGroup parent, int viewType) {
View view= LayoutInflater.from ( mContext ).inflate ( R.layout.users_template ,parent,false);
return new UserAdapter.ViewHolder ( view );
}
@Override
public void onBindViewHolder (@NonNull ViewHolder holder, int position) {
User user=mUsers.get ( position );
holder.username.setText ( user.getFirstName () );
}
@Override
public int getItemCount () {
return mUsers.size ();
}
public class ViewHolder extends RecyclerView.ViewHolder{
public TextView username;
public ViewHolder(View itemView){
super(itemView);
username=itemView.findViewById ( R.id.username1 );
}
}
}
存储名字的用户:
public class User {
public String firstName;
public String secondName;
public String uid;
public String email;
public User(){
}
public User (String firstName, String secondName, String uid, String email) {
this.firstName = firstName;
this.secondName = secondName;
this.uid=uid;
this.email=email;
}
public User (String firstname, String secondname) {
}
@PropertyName ( "FirsName" )
public String getFirstName () {
return firstName;
}
@PropertyName ("SecondName" )
public String getSecondName () {
return secondName;
}
@PropertyName ( "Uid" )
public String getUid () {
return uid;
}
@PropertyName ( "E-mail" )
public String getEmail () {
return email;
}
@PropertyName ( "FirsName" )
public void setFirstName (String firstName) {
this.firstName = firstName;
}
@PropertyName ( "SecondName" )
public void setSecondName (String secondName) {
this.secondName = secondName;
}
@PropertyName ( "Uid" )
public void setUid (String uid) {
this.uid = uid;
}
@PropertyName ( "E-mail" )
public void setEmail (String email) {
this.email = email;
}
}
我的数据库规则是:
{
"rules": {
".read": "true",
".write":"true",
"users":{
"$user_id":{
".read": "auth.uid === $user_id",
".write": "auth.uid === $user_id"
}
}
}
}
在调试模式下,向我显示了此信息:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.sportsbuddy, PID: 662
com.google.firebase.database.DatabaseException: Found two getters or fields with conflicting case sensitivity for property: secondname
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.addProperty(com.google.firebase:firebase-database@@17.0.0:540)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.<init>(com.google.firebase:firebase-database@@17.0.0:471)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.loadOrCreateBeanMapperForClass(com.google.firebase:firebase-database@@17.0.0:312)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertBean(com.google.firebase:firebase-database@@17.0.0:413)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(com.google.firebase:firebase-database@@17.0.0:214)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertToCustomClass(com.google.firebase:firebase-database@@17.0.0:79)
at com.google.firebase.database.DataSnapshot.getValue(com.google.firebase:firebase-database@@17.0.0:212)
at com.example.sportsbuddy.Fragments.UsersFragment$1.onDataChange(UsersFragment.java:60)
at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@17.0.0:75)
at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@17.0.0:63)
at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@17.0.0:55)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:6986)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1445)
主要错误是在UsersFragment中的onDataChange。
我希望向我展示我在数据库中拥有的所有用户。
答案 0 :(得分:2)
您的错误提示:
Found two getters or fields with conflicting case sensitivity for property: secondname
为了解此错误的含义,您需要了解Firebase如何序列化和反序列化从数据库接收的数据。
Firebase使用字段名称以及getter和setter来序列化和反序列化对象,这意味着,每当您从数据库中接收JSON对象时,firebase就会将这些键值与getter / setter以及您字段中的字段进行映射POJO类。
要消除此错误,您必须更改字段名称,即
public String secondName;
与数据库中的其他内容 OR 不同,您需要更改getter和setter函数的名称。由于Firebase两次看到该属性,因此出现错误,一次在您的字段中,第二次在您的getter / setter中。
此外,请尝试将变量设为私有。这可能会有所帮助。另外,如何将数据库中的“电子邮件”更改为简单的“电子邮件”?
答案 1 :(得分:0)
不知道确切的错误将很难说。
但是我认为是当您将firebase对象转换为用户类时。在您的Firebase中,字段电子邮件为“电子邮件”,在您的班级中为“电子邮件”。对其进行更改以匹配您的firebase数据库,或者进行相反的更改(更改firebase字段以匹配您的用户类)。
发布您的完整错误,以便我们能够提供更多信息。
---编辑---
它在firebase数据库和您的用户类之间区分大小写。更改字段以使其彼此匹配,您会没事的。
答案 2 :(得分:0)
从以下位置更改构造函数
public User (String firstname, String secondname) {
}
到
public User (String firstName, String secondName) {
}
此外,您的Firebase密钥与模型类密钥不匹配。
您的FirebaseKey为FirstName
,而您的模型类密钥为firstName
。解决此问题(与SecondName
和secondName
相同)!
答案 3 :(得分:0)
您的领域和获取者/设定者都是公开的。这意味着Firebase看到两个属性:
secondname for the field
SecondName for the getter/setter
由于目前尚不清楚如何将Java类从数据库映射到JSON,因此会引发此错误:
发现两个具有属性区分大小写的吸气剂或字段
最简单的解决方法是将字段设为私有:
public class User {
private String firstName;
private String secondName;
private String uid;
private String email;
public User(){
}
...