dataSnapshop.getValue(MyClass.class)关闭我的应用程序 - 我无法从Firebase数据库中读取

时间:2017-07-29 18:31:45

标签: android database firebase

我正在制作一个与Firebase相关联的Android应用。我是Firebase和Android应用开发的新手。从数据库中的同一个类添加数据效果很好,但读取该数据会关闭我的应用程序。

下面是我的logcat:     07-29 20:20:19.376 23784 23784 D AndroidRuntime:关闭VM     07-29 20:20:19.378 23784 23784 E AndroidRuntime:致命异常:主要     07-29 20:20:19.378 23784 23784 E AndroidRuntime:处理:,PID:23784     07-29 20:20:19.378 23784 23784 E AndroidRuntime:com.google.firebase.database.DatabaseException:类缺少不带参数的构造函数 以下是我的代码:

public class NewFragment extends Fragment {

    ....
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.new, container, false);
.....

        final FirebaseDatabase database = FirebaseDatabase.getInstance();
        DatabaseReference simpleClassDatabaseReference = database.getReference().child("SimpleClass");
......    
            ChildEventListener childEventListener = new ChildEventListener() {
                @Override
                public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                    SimpleClass simpleClass = dataSnapshot.getValue(SimpleClass.class);
                }

                @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) {

                }
            };simpleClassDatabaseReference.addChildEventListener(childEventListener);

        }
        return v;
    }
}

这是我的简单课程:

public class SimpleClass {
    String simpleWord;
    String secondWord;

    public SimpleClass (String simpleWord, String secondWord){
        this.simpleWord = simpleWord;
        this.secondWord = secondWord;
    }

    public String getSimpleWord() {
        return simpleWord;
    }

    public String getSecondWord() {
        return secondWord;
    }
}

这就是我在Firebase中编写该类的方法:

SimpleClass simpleClass = new SimpleClass("HI!!", "Hi again!!");
DatabaseReference mSimpleClassDatabaseReference = database.getReference().child("SimpleClass");
mSimpleClassDatabaseReference.push().setValue(simpleClass);

1 个答案:

答案 0 :(得分:1)

所以要解决这个问题:

public class SimpleClass {
    String simpleWord;
    String secondWord;

    public SimpleClass (){
        //construtor with no arguments
    }

    public SimpleClass (String simpleWord, String secondWord){
    this.simpleWord = simpleWord;
    this.secondWord = secondWord;
}

    public String getSimpleWord() {
        return simpleWord;
    }

    public String getSecondWord() {
        return secondWord;
    }
}

你的错误是简单的类nedds一个没有参数的construtor。