使用RecyclerView获取资源$ NotFoundException

时间:2017-01-23 17:27:39

标签: android android-recyclerview

我收到以下错误:

android.content.res.Resources$NotFoundException: String resource ID #0x0 

我只是使用RecyclerView创建一个数字列表:

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

public class SongRVAdapter extends RecyclerView.Adapter<SongViewHolder> {
    private Context context;

    public SongRVAdapter(Context context) {
        this.context = context;
    }


    @Override
    public SongViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.song_info_row, parent, false);

        return new SongViewHolder(view);
    }

    @Override
    public void onBindViewHolder(SongViewHolder holder, int position) {
        Log.d("CodeKamp", "onBindViewHolder called for position " + position);
        holder.titleTextView.setText(position);
        holder.durationTextView.setText("00:" + position);
    }

    @Override
    public int getItemCount() {
        return 10;
    }
}

1 个答案:

答案 0 :(得分:6)

您无法将int值传递给setText(),否则会尝试查找与int ID匹配的字符串资源。所以

holder.titleTextView.setText(position);

需要更改为

holder.titleTextView.setText(String.valueOf(position));

position转换为字符串。