Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 是否仍然可以访问特定的firebase子项';s值,如i';你用过吗?_Java_Android_Firebase_Firebase Realtime Database - Fatal编程技术网

Java 是否仍然可以访问特定的firebase子项';s值,如i';你用过吗?

Java 是否仍然可以访问特定的firebase子项';s值,如i';你用过吗?,java,android,firebase,firebase-realtime-database,Java,Android,Firebase,Firebase Realtime Database,我有一个firebase项目,用户以孩子为“问题”和其他价值观。我通过将值设置为类对象上传了问题的孩子 Question question = new Question(qID, uID, text, subject, imageLink, date, time, points, containsMedia); FirebaseDatabase.getInstance().getReference() .child("Users") .child(FirebaseAuth.get

我有一个firebase项目,用户以孩子为“问题”和其他价值观。我通过将值设置为类对象上传了问题的孩子

Question question = new Question(qID, uID, text, subject, imageLink, date, time, points, containsMedia);
FirebaseDatabase.getInstance().getReference()
    .child("Users")
    .child(FirebaseAuth.getInstance().getCurrentUser().getUid())
    .child("Questions")
    .child(qID)
    .setValue(question);
我想作为类对象访问此子对象。我想检索所有的子节点值、主题、时间和提供的所有信息

我在主要活动中使用了以下代码:


    FirebaseDatabase.getInstance().getReference().child("Users").addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

                if (dataSnapshot.hasChild("Questions"))
                {

                        for (DataSnapshot d : dataSnapshot.getChildren())
                        {

                            String parent = d.getKey();

                            if (parent.equals("Questions"))
                            {
                                for (DataSnapshot question_object: d.getChildren())
                                {
                                    questions_list.add(question_object.getValue(Question.class));
                                }
                            }
                        }
                }

这是我的问题课<代码>问题.java

public class Question {
    public String qID, uID;
    public String text, subject, imageLink, date, time, points, containsMedia;

    public Question(String qID, String uID, String text, String subject, String imageLink, String date, String time, String points, String containsMedia) {
        this.qID = qID;
        this.uID = uID;
        this.text = text;
        this.subject = subject;
        this.imageLink = imageLink;
        this.date = date;
        this.time = time;
        this.points = points;
        this.containsMedia = containsMedia;
    }


    public String getqID() {
        return qID;
    }

    public String getuID() {
        return uID;
    }

    public String getText() {
        return text;
    }

    public String getSubject() {
        return subject;
    }

    public String getImageLink() {
        return imageLink;
    }

    public String getDate() {
        return date;
    }

    public String getTime() {
        return time;
    }

    public String getPoints() {
        return points;
    }

    public String getContainsMedia() {
        return containsMedia;
    }
}
下面是日志:


    com.google.firebase.database.DatabaseException: Class com.solvify.QA.Question does not define a no-argument constructor. If you are using ProGuard, make sure these constructors are not stripped.
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.deserialize(com.google.firebase:firebase-database@@16.0.4:557)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.deserialize(com.google.firebase:firebase-database@@16.0.4:550)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertBean(com.google.firebase:firebase-database@@16.0.4:420)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(com.google.firebase:firebase-database@@16.0.4:214)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertToCustomClass(com.google.firebase:firebase-database@@16.0.4:79)
        at com.google.firebase.database.DataSnapshot.getValue(com.google.firebase:firebase-database@@16.0.4:212)
        at com.solvify.MainActivity$3.onChildAdded(MainActivity.java:138)
        at com.google.firebase.database.core.ChildEventRegistration.fireEvent(com.google.firebase:firebase-database@@16.0.4:79)
        at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@16.0.4:63)
        at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@16.0.4:55)
        at android.os.Handler.handleCallback(Handler.java:754)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:163)
        at android.app.ActivityThread.main(ActivityThread.java:6227)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
2020-04-21 11:42:22.823 369-2482/? E/ANDR-PERF-RESOURCEQS: Failed to apply optimization [4, 0]
2020-04-21 11:42:23.131 1470-1591/? E/NetdConnector: NDC Command {766 bandwidth removeiquota rmnet_data0} took too long (583ms)


您在JSON中迭代的级别太深了。我始终发现,通过按照快照变量的迭代级别命名快照变量,可以最容易地防止这种错误:

FirebaseDatabase.getInstance().getReference().child("Users").addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(@NonNull DataSnapshot userSnapshot, @Nullable String s) {
            if (userSnapshot.hasChild("Questions")) {
                for (DataSnapshot questionSnapshot: userSnapshot.child("Questions").getChildren()) {
                     questions_list.add(questionSnapshot.getValue(Question.class));
                }
            }
        }
        ...

addchildEvent是循环,直到您无法获取addchildEventListner中所需的所有Child无需额外的for循环,使用此代码获取数据库的所有Child

FirebaseDatabase.getInstance().getReference().child("Users").addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

            if (dataSnapshot.hasChild("Questions"))
            {
                questions_list.add(datasnapshot.getValue(Question.class));
            }
        }

特别感谢所有提出建议的人。我重新仔细阅读了日志并解决了问题。 我为我的类添加了另一个没有参数的构造函数。它解决了我的问题。 实际上,此错误是由于缺少默认构造函数造成的

我只是加了一句

 public Question()
    {

    }

它解决了这个问题。

用户的子节点是什么?这就是当前用户UID,对吗?什么是用户的父节点?@Dharmaraj没有用户的父节点..请发布您的错误,我还建议使用问题类对象的生成器模式,会很好的,对不起,我忘记添加我的日志猫…等等,我要更新我的问题..已经尝试过了。但是面对同样的错误有什么错误??