【问题标题】:Getting an error "found conflicting getters" when trying to upload a message to a firebase database尝试将消息上传到 Firebase 数据库时出现“发现冲突的 getter”错误
【发布时间】:2018-02-20 14:02:28
【问题描述】:

我正在使用这个库:https://github.com/stfalcon-studio/ChatKit 将聊天添加到我的应用程序,当我尝试将消息上传到 firestore db 时出现此错误:

FATAL EXCEPTION: main
                  Process: neoncore.com.onepiece, PID: 3907
                  java.lang.RuntimeException: Found conflicting getters for name getUser on class neoncore.com.onepiece.beans.chat.Message
                      at com.google.android.gms.internal.zzevb$zza.<init>(Unknown Source)
                      at com.google.android.gms.internal.zzevb.zzg(Unknown Source)
                      at com.google.android.gms.internal.zzevb.zza(Unknown Source)
                      at com.google.android.gms.internal.zzevb.zzbw(Unknown Source)
                      at com.google.firebase.firestore.zzk.zzcd(Unknown Source)
                      at com.google.firebase.firestore.CollectionReference.add(Unknown Source)
                      at neoncore.com.onepiece.activities.ChatDetail$1.onSubmit(ChatDetail.java:57)
                      at com.stfalcon.chatkit.messages.MessageInput.onSubmit(MessageInput.java:145)
                      at com.stfalcon.chatkit.messages.MessageInput.onClick(MessageInput.java:108)
                      at android.view.View.performClick(View.java:5210)
                      at android.view.View$PerformClick.run(View.java:21328)
                      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:5551)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)

这是我更新的消息类:

public class Message implements IMessage,
        MessageContentType.Image, /*this is for default image messages implementation*/
        MessageContentType /*and this one is for custom content type (in this case - voice message)*/ {

    private String id;
    private String text;
    private Date createdAt;
    private User user;
    private Image image;
    private Voice voice;

    public Message(String id, User user, String text) {
        this(id, user, text, new Date());
    }

    public Message(String id, User user, String text, Date createdAt) {
        this.id = id;
        this.text = text;
        this.user = user;
        this.createdAt = createdAt;
    }

    @Override
    public String getId() {
        return id;
    }

    @Override
    public String getText() {
        return text;
    }

    @Override
    public Date getCreatedAt() {
        return createdAt;
    }

    @Override
    public User getUser() {
        return this.user;
    }

    @Override
    public String getImageUrl() {
        return image == null ? null : image.url;
    }

    public Voice getVoice() {
        return voice;
    }

    public String getStatus() {
        return "Sent";
    }

    public void setText(String text) {
        this.text = text;
    }

    public void setCreatedAt(Date createdAt) {
        this.createdAt = createdAt;
    }

    public void setImage(Image image) {
        this.image = image;
    }

    public void setVoice(Voice voice) {
        this.voice = voice;
    }

    public static class Image {

        private String url;

        public Image(String url) {
            this.url = url;
        }
    }

    public static class Voice {

        private String url;
        private int duration;

        public Voice(String url, int duration) {
            this.url = url;
            this.duration = duration;
        }

        public String getUrl() {
            return url;
        }

        public int getDuration() {
            return duration;
        }
    }
}

还有我的用户类:

     [public class User implements IUser {


    private String id, name, avatar;

    public User(String id, String name, String avatar) {
        this.id = id;
        this.name = name;
        this.avatar = avatar;
    }

    public User() {
    }


    @Override
    public String getId() {
        return id;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public String getAvatar() {
        return avatar;
    }
}][1]

这是发生错误的地方:

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat_detail);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        FirebaseUser currentUser = FirebaseAuth.getInstance().getCurrentUser();
        firebaseFirestore = FirebaseFirestore.getInstance();
        final Author author = new Author(currentUser.getUid(),currentUser.getDisplayName(),currentUser.getPhotoUrl().toString());
        messageMessagesListAdapter = new MessagesListAdapter<>(author.getId(),null);
        messagesList = findViewById(R.id.messagesList);


        inputView = findViewById(R.id.input);
        inputView.setInputListener(new MessageInput.InputListener() {
            @Override
            public boolean onSubmit(CharSequence input) {
                CollectionReference reference = firebaseFirestore.collection("Messages");
                final Message message = new Message(author,input.toString(), "",null,new Date());
                reference.add(message).addOnCompleteListener(new OnCompleteListener<DocumentReference>() {
                    @Override
                    public void onComplete(@NonNull Task<DocumentReference> task) {

                        messageMessagesListAdapter.addToStart(message,true);
                    }
                }).addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        Log.d("MessageUPloadFailed",e.getMessage());
                    }
                });
                return true;
            }
        });

        messagesList.setAdapter(messageMessagesListAdapter);
    }

错误发生在我尝试将消息添加到调用 add 方法的 firestore 数据库时。 database image

【问题讨论】:

  • 为什么你的getter方法上有@Override注解?
  • 实现接口。

标签: java class firebase android-recyclerview google-cloud-firestore


【解决方案1】:

您收到此错误:

java.lang.RuntimeException: Found conflicting getters for name getUser on class Message

因为在Message 类中,您有一个getUser() 方法,但您根本没有user 字段。相反,您有一个 author 字段,该字段的 getter 应该是:

@Override
public Author getAuthor() { //NOT getUser()
    return author;
}

【讨论】:

  • 你的回答完全有道理,但我仍然遇到同样的错误,我将作者类名称更改为用户,字段名称也更改为用户,但我仍然遇到同样的错误
  • 您仍然收到相同的错误,因为您的数据库中很可能有相同的数据。删除旧数据,添加新数据,然后重试。有用吗?
  • 我已经在 firebase 中清除了我的数据库..我仍然遇到同样的错误..
  • 请向我展示您更改的数据库和更新的代码,以便看得更清楚。您可以简单地编辑您的问题。
  • 我已经更新了代码和数据库的图像
猜你喜欢
  • 1970-01-01
  • 2020-07-13
  • 2017-08-20
  • 1970-01-01
  • 2017-01-11
  • 2017-07-13
  • 2012-06-16
  • 2014-02-26
  • 2021-06-23
相关资源
最近更新 更多