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

问题描述

我正在尝试从Firebase数据库检索数据,并将其显示在ListView的TextViews中。但是,我在此行上遇到缺少构造函数异常: Clientsclients = clientsSnapshot.getValue(Clients.class);

I'm trying retrieve data from my firebase database and display it in TextViews in a ListView. However, I'm getting a 'missing constructor' exception on this line: Clients clients = clientsSnapshot.getValue(Clients.class);.

这里是完整的例外:

Here's the full exception:

不确定如何修复它。我在Firebase或android开发方面都不是很有经验,所以我们将不胜感激。
干杯!

Not sure how to fix it. I'm not very experienced in either firebase or android development so any help would be greatly appreciated.Cheers guys!

public class ViewProfile extends AppCompatActivity{

    ListView listviewClients;
    List<Clients> clientsList;

    DatabaseReference databaseClients;

    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_profile);

        listviewClients = (ListView) findViewById(R.id.listviewClients);
        databaseClients = FirebaseDatabase.getInstance().getReference("clients");

        clientsList = new ArrayList<>();
    }

    @Override
    protected void onStart() {
        super.onStart();

        databaseClients.addValueEventListener (new ValueEventListener() {


            @Override
            public void onDataChange(com.google.firebase.database.DataSnapshot dataSnapshot) {
                clientsList.clear();

                for (com.google.firebase.database.DataSnapshot clientsSnapshot : dataSnapshot.getChildren()) {
                    Clients clients = clientsSnapshot.getValue(Clients.class);

                    clientsList.add(clients);
                }
                ClientsList adapter = new ClientsList(ViewProfile.this, clientsList);
                listviewClients.setAdapter(adapter);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }

        });
    }
}

Clients.java:

public class Clients {

    String clientName;
    String clientCareer;
    Boolean techCB;
    Boolean mediCB;
    Boolean renewableEnergyNF;
    Boolean googleNF;
    Boolean novartisNF;
    Boolean teslaNF;
    Boolean fbLRS;
    Boolean applLRS;
    Boolean yhooLRS;
    Boolean eurusdCB;
    Boolean usdrubCU;
    Boolean silverCO;
    Boolean goldCO;
    Boolean gbpusdCU;
    Boolean nsdqI;
    Boolean sp500I;


//    public Clients(String id, String name, String career, Boolean techCB, Boolean mediCB, Boolean renewableEnergyNF, Boolean googleNF, Boolean novartisNF, Boolean teslaNF, Boolean fbLRS, Boolean applLRS, Boolean yhooLRS, Boolean eurusdCB, Boolean usdrubCU, Boolean silverCO, Boolean goldCO, Boolean gbpusdCU, Boolean nsdqI, Boolean sp500I) {
//
//    }

    public Clients(String id, String clientCareer, String clientName, Boolean techCB, Boolean mediCB, Boolean renewableEnergyNF,
                   Boolean googleNF, Boolean novartisNF, Boolean teslaNF, Boolean fbLRS, Boolean applLRS, Boolean yhooLRS, Boolean eurusdCB,
                   Boolean usdrubCU, Boolean silverCO, Boolean goldCO, Boolean gbpusdCU, Boolean nsdqI, Boolean sp500I) {

        this.clientName = clientName;
        this.clientCareer = clientCareer;
        this.techCB = techCB;
        this.mediCB = mediCB;
        this.renewableEnergyNF = renewableEnergyNF;
        this.googleNF = googleNF;
        this.novartisNF = novartisNF;
        this.teslaNF = teslaNF;
        this.fbLRS = fbLRS;
        this.applLRS = applLRS;
        this.yhooLRS = yhooLRS;
        this.eurusdCB = eurusdCB;
        this.usdrubCU = usdrubCU;
        this.silverCO = silverCO;
        this.goldCO = goldCO;
        this.gbpusdCU = gbpusdCU;
        this.nsdqI = nsdqI;
        this.sp500I = sp500I;
    }

    public String getClientName() {
        return clientName;
    }

    public String getClientCareer() {
        return clientCareer;
    }

    public Boolean getTechCB() {
        return techCB;
    }

    public Boolean getMediCB() {
        return mediCB;
    }

    public Boolean getRenewableEnergyNF() {
        return renewableEnergyNF;
    }

    public Boolean getGoogleNF() {
        return googleNF;
    }

    public Boolean getNovartisNF() {
        return novartisNF;
    }

    public Boolean getTeslaNF() {
        return teslaNF;
    }

    public Boolean getFbLRS() {
        return fbLRS;
    }

    public Boolean getApplLRS() {
        return applLRS;
    }

    public Boolean getYhooLRS() {
        return yhooLRS;
    }

    public Boolean getEurusdCB() {
        return eurusdCB;
    }

    public Boolean getUsdrubCU() {
        return usdrubCU;
    }

    public Boolean getSilverCO() {
        return silverCO;
    }

    public Boolean getGoldCO() {
        return goldCO;
    }

    public Boolean getGbpusdCU() {
        return gbpusdCU;
    }

    public Boolean getNsdqI() {
        return nsdqI;
    }

    public Boolean getSp500I() {
        return sp500I;
    }


推荐答案

在您的Model类中添加:无参数构造函数

In your Model class add : no argument constructor

public class Clients {
    private String name;  //your variables
    ...
    ...

    public Clients (){ }  //no argument constructor

这篇关于DatabaseException:类缺少没有参数的构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 13:29