What causing this exception java.lang.RuntimeException: java.lang.NoSuchMethodException: <init> [class android.view.View]

≯℡__Kan透↙ 提交于 2019-12-01 16:06:18

问题


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:


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.


来源:https://stackoverflow.com/questions/36972335/what-causing-this-exception-java-lang-runtimeexception-java-lang-nosuchmethodex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!