What causing this exception java.lang.RuntimeExcep

2019-02-21 21:43发布

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.

1条回答
Viruses.
2楼-- · 2019-02-21 21:50

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.
查看更多
登录 后发表回答