导致此异常的原因 java.lang.RuntimeException: java.lang.NoSuchMethodException: <init>[类android.view.View] [英] What causing this exception java.lang.RuntimeException: java.lang.NoSuchMethodException: <init> [class android.view.View]
问题描述
我的 firebase
项目有问题.我按照 firebase
GitHub 文档中的步骤操作,但出现此异常
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]
这是一个 ViewHolder
类,它不是内部类.
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);
}
}
这是与firebase相关的代码
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);
我正在使用 firebaseRecyclerView
来填充数据模型,并且 viewHolder
类不是我活动的内部类
I'm using firebaseRecyclerView
to populate data model,
and the viewHolder
class is not inner class of my activity
注意:当包含 recyclerView
的 Activity 启动时会发生异常.
Note: the exception occurs when the activity that contains the recyclerView
starts.
推荐答案
很可能你的自定义 ViewHolder
子类是:
Most likely your custom ViewHolder
subclass is:
- 缺少构造函数
public MyViewHolder(View itemView) {... }
OR - 该类定义在另一个类中,在这种情况下,您需要将其标记为 static
public static class MyViewHolder
.
这篇关于导致此异常的原因 java.lang.RuntimeException: java.lang.NoSuchMethodException: <init>[类android.view.View]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!