2017-04-19 56 views
0

我只是分享相關代碼:FirebaseUI NoSuchMethodException:

MainActivity:

RecyclerView recyclerView; 

     DatabaseReference databaseReference; 




    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_todo); 
     Firebase.setAndroidContext(this); 


     databaseReference = FirebaseDatabase.getInstance().getReference(); 

     recyclerView = (RecyclerView) findViewById(R.id.list); 
     recyclerView.setHasFixedSize(true); 



     LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this); 
     linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL); 
     recyclerView.setLayoutManager(linearLayoutManager); 





     FirebaseRecyclerAdapter<Grupo,GruposAdapter.ViewHolder> adapter = 
       new FirebaseRecyclerAdapter<Grupo, GruposAdapter.ViewHolder>(
        Grupo.class, 
         R.layout.item, 
         GruposAdapter.ViewHolder.class, 
         databaseReference.child("Grupos") 


     ) { 
      @Override 
      protected void populateViewHolder(GruposAdapter.ViewHolder viewHolder, Grupo model, int position) { 
        viewHolder.id.setText(String.valueOf(model.getId())); 
        viewHolder.grupo.setText(model.getGroupname()); 
      } 
     }; 



     recyclerView.setAdapter(adapter); 
    } 

錯誤:

04-19 14:50:52.886 22120-22120/datos.agregar.firebase.ejemplo.com.ejemploagregardatoscffirebase E/AndroidRuntime: FATAL EXCEPTION: main 
                                Process: datos.agregar.firebase.ejemplo.com.ejemploagregardatoscffirebase, PID: 22120 
                                java.lang.RuntimeException: java.lang.NoSuchMethodException: <init> [class android.view.View] 

爲什麼NoSuchMethodException?想法?

public static class ViewHolder extends RecyclerView.ViewHolder{ 
     public TextView id,grupo; 
     public CardView cardview; 

     public ViewHolder(View itemView){ 
      super(itemView); 

      id = (TextView) itemView.findViewById(R.id.idgrupo); 
      grupo = (TextView) itemView.findViewById(R.id.grupo); 
      cardview = (CardView) itemView.findViewById(R.id.cardview); 
     } 
    } 

回答

1

好像你GruposAdapter.ViewHolder缺少所需的構造:

有固定。見https://github.com/firebase/FirebaseUI-Android/issues/46#issuecomment-167373575。從那裏:

There are two likely causes for this:

  • make sure that your TaskViewHolder class is public *if your TaskViewHolder is an inner class of e.g. your activity, make sure it's static

So likely

public static TaskViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { 
+0

嗨,你是正確的,固定與內部類像靜態,但爲什麼?爲什麼我需要靜態類視圖? – josedlujan

+0

因爲您將其作爲內部類添加到另一個類中。 Java的增加了一個隱含的字段,構造函數參數,這樣的類來引用包含的對象(例如,這時候你是指'MainActivity.this'在單擊處理您所使用的)。將「static」添加到該類將刪除該字段/參數。 –