用户未定义没有参数的构造函数

用户未定义没有参数的构造函数

本文介绍了用户未定义没有参数的构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直收到此错误用户没有定义无参数的构造函数.如果您使用的是ProGuard,请确保没有剥离这些构造函数."尝试了一切,不知道为什么会发生.

i kept getting this error"users does not define a no-argument constructor. If you are using ProGuard, make sure these constructors are not stripped." Tried everything, no idea why it happen.

public void retrievingUserInfo(){

    databaseUserID.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            //clearing the previous userinfo list
            Users_Info.clear();

            //iterating through all the nodes
            for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                //getting userinfo
                users userinfo = postSnapshot.getValue(users.class);
                //adding userinfo to the list
                Users_Info.add(userinfo);
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}

users.class

users.class

@Keep
public class users {

    public String user_id, address, contact, name;

    public users(String user_id, String address, String contact,String name)
    {}


}

推荐答案

JavaBeans 需要无参数的构造函数.

JavaBeans require a no-argument constructor to be present.

当Java类根本没有构造函数时,编译器会自动向其添加默认的no-arg构造函数.在类中定义任何构造函数后,默认的no-arg构造函数就会消失.

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

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

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

public users(String user_id, String address, String contact,String name)
{}

只要存在该构造函数,并且您没有定义no-arg构造函数,该类就不会有一个.

As long as that constructor is present, and you don't define a no-arg constructor, that class will not have one.

要解决此问题,您需要从类中删除该构造函数,或者手动向其添加无参数构造函数:

To resolve this, you either need to remove that constructor from the class, or manually add a no-arg constructor to it:

public users() {}

这篇关于用户未定义没有参数的构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 14:33