本文介绍了从 Firebase 获取数据到 RecyclerView 时出错.致命异常:...没有定义无参数构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设计了 Activity,它从 firebase Db 获取数据并将其显示在回收器视图中,但是当我运行它时会出现以下错误

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

我的代码是

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

        }
    });

}
}

我处理数据的模态类(RvClass)代码是

My modal class(RvClass) code for handling data is

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;
}
}

我的 Firebase 架构是

My firebase schema is

我做错了什么请帮助我找出错误?谢谢!

what is the mistake I am doing please help me in figuring out the bug?Thanks!

推荐答案

您收到以下错误:

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

因为您的 RvClass 类没有定义 无参数构造函数.

Because your RvClass class does not define a no-argument constructor.

JavaBeans 需要无参数构造函数出席.

JavaBeans require a no-argument constructor to be present.

在Java中,当一个类根本没有构造函数时,编译器会自动添加一个默认的无参数构造函数.在类中定义任何构造函数的那一刻,默认的无参数构造函数就会消失.

In Java, when a class has no constructors at all, there is a default no-argument constructor automatically added by the compiler. The moment you define any constructor in the class, the default no-argument constructor goes away.

在您的代码中,您的 RvClass 类定义了这样一个包含三个参数的构造函数:

In your code, your RvClass class defines such a constructor that contains three arguments:

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() {}

当 Firebase 实时数据库 SDK 反序列化来自数据库的对象时,它要求任何正在使用的对象都具有此构造函数,以便它可以使用它来实例化对象.对象中的字段通过使用公共 setter 方法或直接访问公共成员来设置.

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. Fields in the objects are set by using public setter methods or direct access to public members.

如果您的 RvClass 类没有公共无参数构造函数,则 SDK 并不真正知道如何创建它的实例.所以必须拥有它.

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. So it is mandatory to have it.

另外请注意 setter 和 getter 不是必需的.Setter 始终是可选的,因为如果 JSON 属性没有 setter,Firebase 客户端会将值直接设置到字段上.也不需要带参数的构造函数.两者都是惯用的,没有它们的类也有很好的例子.如果您将字段设为公开,则 getter 也是可选的.

Also please note that setters and getter are not required. 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. 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.

这篇关于从 Firebase 获取数据到 RecyclerView 时出错.致命异常:...没有定义无参数构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 04:52