2017-04-15 102 views
1

我試圖從我的firebase數據庫檢索數據並將其顯示在ListView中的TextView中。但是,我在這一行上收到'缺少構造函數'異常:Clients clients = clientsSnapshot.getValue(Clients.class);DatabaseException:類缺少一個沒有參數的構造函數

下面是完整的異常: enter image description here

不知道如何解決它。我在Firebase或Android開發方面都不是很有經驗,所以任何幫助都將不勝感激。 乾杯傢伙!

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

'客戶端類「沒有無參數構造函數 – EpicPandaForce

+1

您需要哪些幫助?你知道什麼是構造函數嗎?你知道什麼是論據嗎?一旦你理解了術語,你應該能夠弄清楚如何解決這個錯誤。 –

+0

發佈「客戶」類代碼.. – rafsanahmad007

回答

7

在模型中添加類:無參數constructor

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

    public Clients(){ } //no argument constructor 
+0

謝謝!這爲我修好了。再次感謝。 – Joe

2

我知道的論點是什麼,他們是像參數

是的,參數和參數是相同的東西。

我不知道構造函數的定義,但它通常是公共_____(){}?

是的,這是正確的。錯誤消息告訴你,你的Clients類沒有沒有任何參數的構造函數。如果您創建的類沒有構造函數,則會自動生成無參數構造函數。你會得到錯誤,因爲你有一個帶有參數的構造函數Clients。因此,您需要明確提供一個不帶任何參數的參數,以使其能夠正確使用Firebase。

p.s.您應該使用boolean而不是Boolean。有關更多詳細信息,您應該瞭解更多關於基本類型和包裝器對象之間的區別。

+1

感謝您的所有信息。我只是試圖讓自己沉浸其中,這非常有幫助!歡呼,祝你有美好的一天。 – Joe

相關問題