Getting Resources$NotFoundException with RecyclerView [duplicate]

Refresh

December 2018

Views

523 time

1

This question already has an answer here:

I'm getting the following error:

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

I am just creating a number list using 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 answers

4

You can't pass int values to setText(), else it will try and look up a String resource matching that int ID. So

holder.titleTextView.setText(position);

needs to be changed to

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

to convert position into a String.