8

I'm having a problem with my firebase project. I followed the steps on firebase GitHub documentation, but I got this exception

java.lang.RuntimeException: java.lang.NoSuchMethodException: <init>
[class android.view.View]

this is a ViewHolder class which is not an inner class.

public class ProductViewHolder extends RecyclerView.ViewHolder{
public View mView;
public ImageView img;
public TextView title;
public TextView price;
public RatingBar stars;


ProductViewHolder(View itemView) {
    super(itemView);

    mView = itemView;
    img = (ImageView) itemView.findViewById(R.id.productImg);
    title = (TextView) itemView.findViewById(R.id.txtTitle);
    price = (TextView) itemView.findViewById(R.id.txtPrice);
    stars = (RatingBar) itemView.findViewById(R.id.ratingBar);
    }
}

and this is the firebase related code

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

    // Recycler adapter
    FirebaseRecyclerAdapter<Product, ProductViewHolder> adapter =
            new FirebaseRecyclerAdapter<Product, ProductViewHolder>(
                    Product.class,
                    R.layout.product_list_item,
                    ProductViewHolder.class,
                    firebaseRef.child("product")) {

                @Override
                protected void populateViewHolder(ProductViewHolder productViewHolder, Product product, int i) {
                    Picasso.with(ShopsApp.getLyShopsAppContext())
                            .load(product.getImgUrl())
                            .placeholder(R.drawable.none)
                            .into(productViewHolder.img);
                    productViewHolder.title.setText(product.getTitle());
                    productViewHolder.price.setText(product.getPrice());
                    productViewHolder.stars.setRating(4.0f);
                }
            };

    recyclerView1.setAdapter(adapter);

I'm using firebaseRecyclerView to populate data model, and the viewHolder class is not inner class of my activity

Note: the exception occurs when the activity that contains the recyclerView starts.

Aditya Vyas-Lakhan
  • 13,409
  • 16
  • 61
  • 96
Abdalltif Basher
  • 617
  • 7
  • 19

1 Answers1

20

Most likely your custom ViewHolder subclass is:

  • missing a constructor public MyViewHolder(View itemView) {... } OR
  • the class is defined inside another class, in which case you need to mark it as static public static class MyViewHolder.
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Hi @Frank van Puffelen, Could you explain why the class need to be static? Thank in advance! – Jaco Jul 15 '17 at 19:30
  • 1
    That's only needed when the POJO is an inner class in another class. In that case Java adds a hidden field to the class and parameter to the default constructor that allows the object to find its container. This parameter makes it that Firebase can't find the default constructor anymore. Marking the class as `static` removes the field and parameter and thus Firebase can find the parameterless constructor again. – Frank van Puffelen Jul 16 '17 at 00:04
  • Great :) Thanks @Frank van Puffelen! – Jaco Jul 16 '17 at 12:38