简体   繁体   English

将数据从 Firebase 提取到 RecyclerView 时出错。 致命异常:...没有定义无参数构造函数

[英]Error in fetching data from Firebase to RecyclerView. FATAL EXCEPTION: ... does not define a no-argument constructor

I have designed activity such that it fetches data from firebase Db and displays it in a recycler view but when I run it following error occurs我已经设计了活动,以便它从 firebase Db 获取数据并将其显示在回收器视图中,但是当我运行它时会出现以下错误

图片

my code is我的代码是

package com.example.android.indiandigitalschool;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import java.util.ArrayList;

public class ReceiveNews1 extends AppCompatActivity {
private RecyclerView rv;
private ArrayList<RvClass> list = new ArrayList<>() ;
private DatabaseReference demo;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_receive_news1);
    rv = findViewById(R.id.rv);
    rv.setHasFixedSize(true);
    rv.setLayoutManager(new LinearLayoutManager(this));

    demo= FirebaseDatabase.getInstance().getReference().child("IDS").child("News");
    demo.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for(DataSnapshot snapshot : dataSnapshot.getChildren()) {
            RvClass rvClass = snapshot.getValue(RvClass.class);//error occurs here
              list.add(rvClass);

            }
            CustomAdapter adapter = new CustomAdapter(ReceiveNews1.this,list);
            rv.setAdapter(adapter);

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

}
}

My modal class(RvClass) code for handling data is我用于处理数据的模态类(RvClass)代码是

package com.example.android.indiandigitalschool;

public class RvClass {
private String title;
private String message;
private String time;

public RvClass(String title, String message, String time) {
    this.title = title;
    this.message = message;
    this.time = time;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getMessage() {
    return message;
}

public void setMessage(String message) {
    this.message = message;
}

public String getTime() {
    return time;
}

public void setTime(String time) {
    this.time = time;
}
}

My firebase schema is我的 Firebase 架构是

图片

what is the mistake I am doing please help me in figuring out the bug?我在做什么错误请帮助我找出错误? Thanks!谢谢!

You are getting the following error:您收到以下错误:

FATAL EXCEPTION: ... does not define a no-argument constructor

Because your RvClass class does not define a no-argument constructor .因为您的RvClass类没有定义no-argument constructor

JavaBeans require a no-argument constructor to be present. JavaBeans 需要一个无参数的构造函数

In Java, when a class has no constructors at all, there is a default no-argument constructor automatically added by the compiler.在 Java 中,当一个类根本没有构造函数时,编译器会自动添加一个默认no-argument constructor The moment you define any constructor in the class, the default no-argument constructor goes away.在类中定义任何构造函数的那一刻,默认的无参数构造函数就会消失。

In your code, your RvClass class defines such a constructor that contains three arguments:在您的代码中,您的RvClass类定义了一个包含三个参数的构造函数:

public RvClass(String title, String message, String time) {}

As long as this constructor is present and you don't define a no-argument constructor, that class will not have one.只要这个构造函数存在并且你没有定义一个无参数的构造函数,那个类就不会有一个。

To resolve this, you either remove that constructor from the class, or manually add a no-argument constructor as shown below:要解决此问题,您可以从类中删除该构造函数,或者手动添加一个无参数构造函数,如下所示:

public RvClass() {}

When the Firebase Realtime database SDK deserializes objects that are coming from the database, it requires that any objects in use, to have this constructor, so it can use it to instantiate the object.当 Firebase Realtime 数据库 SDK 反序列化来自数据库的对象时,它要求所有正在使用的对象都具有此构造函数,以便可以使用它来实例化对象。 Fields in the objects are set by using public setter methods or direct access to public members.对象中的字段是通过使用公共 setter 方法或直接访问公共成员来设置的。

If your RvClass class dosen't have a public no-arg constructor, the SDK doesn't really know how to create an instance of it.如果您的 RvClass 类没有公共的无参数构造函数,则 SDK 并不真正知道如何创建它的实例。 So it is mandatory to have it.所以必须拥有它。

Also please note that setters and getter are not required.另请注意,setter 和 getter 不是必需的。 Setters are always optional because if there is no setter for a JSON property, the Firebase client will set the value directly onto the field. Setter 始终是可选的,因为如果 JSON 属性没有 setter,Firebase 客户端会将值直接设置到字段中。 A constructor-with-arguments is also not required.也不需要带参数的构造函数。 Both are idiomatic and there are good cases to have classes without them.两者都是惯用的,并且有很好的情况下没有它们的类。 If you make the fields public, the getters are optional too.如果您公开这些字段,则 getter 也是可选的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android Firebase数据库异常:未定义无参数构造函数 - Android Firebase Database exception: not define a no-argument constructor Class android.location.Location 没有定义无参构造函数 - Class android.location.Location does not define a no-argument constructor Firebase数据库:尽管存在错误,但仍出现“无参数构造函数”错误 - Firebase Database: Getting “No-Argument Constructor” Error Despite Having It 类没有定义无参数构造函数。 如果您使用 ProGuard,请确保这些构造函数没有被剥离 - Class does not define a no-argument constructor. If you are using ProGuard, make sure these constructors are not stripped Class com.test.test.LibraryBook 未定义无参数构造函数 - Class com.test.test.LibraryBook does not define a no-argument constructor 致命异常:使用RecyclerView从Firebase读取数据时 - Fatal Exception: while reading data from firebase using RecyclerView 无法反序列化 object。 Class 没有定义无参数构造函数。如果您使用 ProGuard,请确保这些构造函数没有被剥离 - Could not deserialize object. Class does not define a no-argument constructor.If you are using ProGuard, make sure these constructors are not stripped 坚持将数据从Firebase提取到我的RecyclerView - Stuck on fetching data from firebase to my RecyclerView RecyclerView 未从 firebase 数据库中获取数据 - RecyclerView not fetching data from firebase database 单击 RecyclerView 后,当我想在新活动中显示详细信息时出现错误。 使用 Firebase - Get error when I want to display detail in new activity after click on RecyclerView. Using Firebase
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM