简体   繁体   English

Android Studio Recycle View在onBindViewHolder方法中发生错误

[英]Android Studio Recycle View got error in method onBindViewHolder

After I implemented the tutorial about Recycle View in Android Studio. 在实现了有关Android Studio中的回收视图的教程之后。

The File Adapter file that i used: 我使用的文件适配器文件:

package com.emilhamep.ilham_hp.diamondmaterial;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.Collections;
import java.util.List;


public class RecycleAdapter extends RecyclerView.Adapter<RecycleAdapter.MyViewHolder>{
private LayoutInflater inflater;
List<Information> data= Collections.emptyList();

public RecycleAdapter(Context context,List<Information> data){
    inflater=LayoutInflater.from(context);
    this.data=data;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view=inflater.inflate(R.layout.custom_row,parent,false);
    MyViewHolder holder = new MyViewHolder(view);
    return holder;
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    Information current=data.get(position);
    holder.title.setText(current.title);
    holder.icon.setImageResource(current.iconId);
}

@Override
public int getItemCount() {
    return data.size();
}

class MyViewHolder extends RecyclerView.ViewHolder{
    TextView title;
    ImageView icon;
    public MyViewHolder(View itemView) {
        super(itemView);
        title= (TextView) itemView.findViewById(R.id.list_item);
        icon= (ImageView) itemView.findViewById(R.id.list_icon);
    }
}
}

I got the error in method onBindViewHoder : 我在onBindViewHoder方法中收到错误:

适配器

Does anyone, had gotten similar error with me when implementing Recycle View before? 以前在实施Recycle View时有人遇到过类似的错误吗?

You have a NullPointerException because holder.title is null . 您有一个NullPointerException,因为holder.titlenull

Check your custom_row.xml , you need to have a TextView with list_item as id ( android:id="@+id/list_item" ). 检查您的custom_row.xml ,您需要具有一个以list_item作为idTextViewandroid:id="@+id/list_item" )。

Or, change list_item in title= (TextView) itemView.findViewById(R.id.list_item); 或者,在title= (TextView) itemView.findViewById(R.id.list_item);更改list_item title= (TextView) itemView.findViewById(R.id.list_item); with the right id of the TextView if it already have one. 具有正确的TextView ID (如果已有的话)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM