【问题标题】:com.google.firebase.database.DatabaseException: Found two getters or fields with conflicting case sensitivity for property: user_namecom.google.firebase.database.DatabaseException:发现两个 getter 或属性的区分大小写冲突的字段:user_name
【发布时间】:2019-01-20 03:51:47
【问题描述】:

此代码在从 Firebase 数据库中提取数据时出错
在行中

UsersForChat usersForChat = dataSnapshot.getValue(UsersForChat.class); com.google.firebase.database.DatabaseException:发现两个 getter 或属性的区分大小写冲突的字段:user_name

public class Users_Display_For_Chat_Activity extends AppCompatActivity {

    FirebaseAuth firebaseAuth;

    FirebaseDatabase firebaseDatabase;
    DatabaseReference databaseReference;

    ArrayList<UsersForChat> users = new ArrayList<UsersForChat>();

    RecyclerView recyclerView;
    UsersDispalyActivtyRecyclerViewAdapter rvadapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat_);

        firebaseAuth = FirebaseAuth.getInstance();

        firebaseDatabase = FirebaseDatabase.getInstance();
        databaseReference = firebaseDatabase.getReference();

        rvadapter = new UsersDispalyActivtyRecyclerViewAdapter(this,users);



        recyclerView = (RecyclerView) findViewById(R.id.rv_UsersForChat);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(rvadapter);

        retrivedata();
    }

    public void retrivedata()
    {
        databaseReference.child("Users").addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                UsersForChat usersForChat = dataSnapshot.getValue(UsersForChat.class);
                users.add(usersForChat);
            }

            @Override
            public void onChildChanged(DataSnapshot dataSnapshot, String s) {

            }

            @Override
            public void onChildRemoved(DataSnapshot dataSnapshot) {

            }

            @Override
            public void onChildMoved(DataSnapshot dataSnapshot, String s) {

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }
}

这里是 Pojo 类 UsersForChat

public class UsersForChat {

    String User_Name,User_PhoneNo,User_Profile_Pic_Url;

    public UsersForChat()
    {

    }

    public UsersForChat(String User_Name, String User_PhoneNo, String User_Profile_Pic_Url) {
        this.User_Name = User_Name;
        this.User_PhoneNo = User_PhoneNo;
        this.User_Profile_Pic_Url = User_Profile_Pic_Url;
    }

    public String getUser_Name() {
        return User_Name;
    }

    public String getUser_PhoneNo() {
        return User_PhoneNo;
    }

    public String getUser_Profile_Pic_Url() {
        return User_Profile_Pic_Url;
    }

    public void setUser_Name(String user_Name) {
        User_Name = user_Name;
    }

    public void setUser_PhoneNo(String user_PhoneNo) {
        User_PhoneNo = user_PhoneNo;
    }

    public void setUser_Profile_Pic_Url(String user_Profile_Pic_Url) {
        User_Profile_Pic_Url = user_Profile_Pic_Url;
    }
}

【问题讨论】:

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


    【解决方案1】:

    如果您在项目初期并且可以进行数据库更改,我建议您根据Java Naming Convention 将所有字段名称、setter 和 getter 更改为小写。

    此外,您需要将所有字段的可见性从 default access 更改为 private access,如下所示:

    private String userName, userPhoneNo, userProfilePicUrl;
    

    还有像这样的setter和getter:

    public void setUserEmail(String userEmail) {this.userEmail = userEmail;}    
    public String getUserEmail() {return userEmail;}
    
    public String getUserPhoneNo() {return userPhoneNo;}
    public void setUserPhoneNo(String userPhoneNo) {this.userPhoneNo = userPhoneNo;}
    
    public String getUserProfilePicUrl() {return userProfilePicUrl;}
    public void setUserProfilePicUrl(String userProfilePicUrl) {this.userProfilePicUrl = userProfilePicUrl;}
    

    您可能会看到,只有 setter 和 getter 有 public access

    如果您无法更改数据库中字段的名称,那么您只需更改文件的可见性。

    【讨论】:

    • 如果数据库字段被命名为“user_name”,这将不起作用。 Firebase SDK 不知道如何将 user_name 映射到 setUserName,没有一些关于如何建立关联的额外线索。
    • 是的,你是对的。我建议Sharad Dadhich按照Java naming convention更改上述文件的名称。
    • 但是更改数据库中所有字段的名称可能会很痛苦,不是吗?
    • 是的,会的。道格,你又是对的。答案变了。再次感谢你! ;)
    【解决方案2】:

    当您创建 POJO 时,习惯上将所有字段设为私有,以便仅通过 getter 和 setter 强制访问。

    private String User_Name,User_PhoneNo,User_Profile_Pic_Url;
    

    此外,传统上,您不会在方法名称和成员中使用下划线,只是驼峰式。但我想这取决于你。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多