导致此异常的原因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]

查看:318
本文介绍了导致此异常的原因java.lang.RuntimeException:java.lang.NoSuchMethodException:< init> [类android.view.View]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 firebase 项目存在问题。
我遵循 firebase GitHub文档上的步骤,但是我得到了这个异常

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

这是一个

  public class ProductViewHolder extends RecyclerView.ViewHolder {
public View mView;
public ImageView img;
公共TextView标题;
public TextView价格;
公开评级吧明星;


ProductViewHolder(查看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相关代码



$ $ $ $ $ $ $ $ $ $ $保护无效onStart(){
super.onStart();

//回收站适配器
FirebaseRecyclerAdapter< Product,ProductViewHolder> adapter =
FirebaseRecyclerAdapter< Product,ProductViewHolder>(
Product.class,
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 类不是我的活动的内部类。当包含 recyclerView 的活动启动时会发生异常。

解决方案

很可能您的自定义 ViewHolder 子类是:


  • 缺少构造函数 public MyViewHolder(View itemView){...} OR

  • 类是在另一个类中定义的,在这种情况下,将其标记为静态 public static class MyViewHolder


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.

解决方案

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.

这篇关于导致此异常的原因java.lang.RuntimeException:java.lang.NoSuchMethodException:&lt; init&gt; [类android.view.View]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆