【问题标题】:Firebase DatabaseException "Can't convert object of type java.util.ArrayList"Firebase DatabaseException“无法转换 java.util.ArrayList 类型的对象”
【发布时间】:2020-03-29 22:55:49
【问题描述】:

Im 收到 Firebase DatabaseException“无法将 java.util.ArrayList 类型的对象转换为 com.example.firebasedeneme.Title 类型”

getValue() 出错了

a = FirebaseDatabase.getInstance().getReference("Users");
        but.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                a.addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                        if(dataSnapshot.child(e.getText().toString()).exists()){
                            String username = e.getText().toString();
                            User user = dataSnapshot.child(username).getValue(User.class);

我保存数据

FirebaseDatabase.getInstance().getReference("Users").child(user.getUsername()).setValue(user);

我的用户类看起来像

public class User implements Comparable<User>,Serializable{
    private String username;
    private String password;
    private String title;
    private int xp;
    private int score;
    private int level;
    private int image;
    private Stack<Title> titles;
    public User() {}
    public User(String username, String password, String title, int xp, int score, int level, int image, Stack<Title> titles){
        this.username = username;
        this.password = password;
        this.level = level;
        this.score = score;
        this.xp = xp;
        this.title = title;
        this.image = image;
        this.titles = titles;
    }

我的 Title Class 看起来像

public class Title {
    private int titleno;
    private String title;
    public Title(int titleno, String title){
        this.title = title;
        this.titleno = titleno;
    }
    public int getTitleno(){
        return this.titleno;
    }
    public String getTitle(){
        return this.title;
    }
}

我该如何解决这个问题?

【问题讨论】:

  • 请仅使用android-studio 标签来回答有关 Android Studio IDE 本身的问题。对于一般的 Android 编程问题,请使用android 标签。
  • 请将您的数据库结构添加为 JSON 文件或至少是屏幕截图。
  • 我添加了我的数据库结构作为截图。
  • Mateo Hervas 的回答是正确的。您的 titles 属性是 List 而不是 Stack
  • 我尝试了 Mateo Hervas 的解决方案,但仍然遇到同样的问题。

标签: android firebase firebase-realtime-database


【解决方案1】:

我认为这是因为 Title 的类型是 Stack 而在 Firebase 中你有一个数组。将您的 Title 变量更改为:

private ArrayList<Title> titles;

如果这不起作用,您还可以将您的 Title 模型类添加到问题中吗?这样我们也许可以提供更好的帮助。

【讨论】:

  • 它没有用。我将我的标题模型添加到问题中,感谢您的建议。
  • 您是否遇到同样的错误?还是另一个?我正在查看您的模型和数据库结构,我认为错误是因为您应该将titleno 变量更改为Long。您的值是 10 位长,这是 int 可以容纳的最大值,将其更改为 long 并告诉我,以便我更新答案
  • 我仍然遇到同样的错误。是的,我尝试将其更改为 long 但没有任何改变。
【解决方案2】:

我在从 firebase 实时数据库中获取数据时也遇到了类似的问题。需要在所有模型类中添加默认构造函数。因此,您还需要在 Title 类中添加默认构造函数,与在 User 类中添加的类似。

public Title(){}

参考https://stackoverflow.com/questions/50114944/why-we-need-an-empty-constructor-to-passing-save-a-data-from-firebase#:~:text=Firebase%20cannot%20figure%20out%20on,to%20fill%20in%20using%20reflection.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-22
    • 1970-01-01
    • 2018-07-27
    • 1970-01-01
    • 2021-02-16
    • 2016-10-21
    • 2020-02-11
    • 2017-01-25
    相关资源
    最近更新 更多