Timelord64 Timelord64 - 1 month ago 7x
Android Question

Resources$NotFoundException trying to set up a RecyclerView in Android Studio

I've been trying to set up a RecyclerView for the past two days, and most of it appears to work as intended. However, whenever I attempt to run my app, it crashes with a fatal exception:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.mad.exercise5, PID: 16634
android.content.res.Resources$NotFoundException: String resource ID #0x1
at android.content.res.Resources.getText(Resources.java:342)
at android.support.v7.widget.ResourcesWrapper.getText(ResourcesWrapper.java:52)
at android.widget.TextView.setText(TextView.java:4244)
at (...)

I have been following the slideview tutorials from YouTube, parts 1-4. In the context of the tutorials, the RecyclerView is being set up in a fragment, which I have not gone in to yet. Just in case the error came from creating the RecyclerView in the main activity, I also tried following a tutorial at StackTips.

To the best of my ability, I have been able to stop the error by commenting out the line that sets the Layout Manager (
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
), but the error appears to originate from the
. Specifically, setting text to my ViewHolder TextViews.

Googling the error, and doing some research, I found lots of cases where this error popped up in other questions and issues. However, the problem always seems to be that the user is calling for a resource, but directly referencing it as an id. That is, they would use '
String string = R.string.string
' instead of '
String string = getString(R.string.string)
'. I have triple checked my standard resource implementation, and am confident I have not missed anything. As far as the tutorials have led me to believe, I am correctly instantiating my TextViews, with the correct implementation of
. The main activity context is passed on initialisation, so it should have no trouble finding the relevant ids.

Why am I receiving a Resources$NotFoundException, in my RecyclerView example activity? The two relevant java classes are as follows:


public class MainActivity extends AppCompatActivity {

private ArrayList<Train> mTrains;
private TrainAdapter mTrainAdapter;
private RecyclerView mRecyclerView;

protected void onCreate(Bundle savedInstanceState) {

// Create the Recycler View
mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

mTrains = FillTrainData();
mTrainAdapter = new TrainAdapter(this, mTrains);

private ArrayList<Train> FillTrainData()
ArrayList<Train> mTempTrainData = new ArrayList<>();
String[] mPlatform = new String[]{"platA", "platB", "platC", "platD"};
String[] mStatus = new String[]{"arrive", "notarrive"};
String[] mDestination = new String[]{"destA", "destB", "destC", "destD"};
String[] mDestinationTime = new String[]{"destA", "destB", "destC", "destD"};
int[] mArrivalTime = new int[]{1,2,3,4};

for(int i = 0; i < 4; i ++)
Train mTrain = new Train(mPlatform[i], mArrivalTime[i], mStatus[i%2],
mDestination[i], mDestinationTime[i]);

return mTempTrainData;


public class TrainAdapter extends RecyclerView.Adapter<TrainAdapter.CustomViewHolder> {

private Context mContext;
private ArrayList<Train> mTrains;

public TrainAdapter(Context context, ArrayList<Train> trains) {
mContext = context;
mTrains = trains;

public CustomViewHolder onCreateViewHolder(ViewGroup parent, int index) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.train_item, null);
CustomViewHolder viewHolder = new CustomViewHolder(view);
return viewHolder;

public void onBindViewHolder(CustomViewHolder viewHolder, int position) {
Train currentTrain = mTrains.get(position);


public int getItemCount() {
return (mTrains != null ? mTrains.size() : 0);

class CustomViewHolder extends RecyclerView.ViewHolder {
TextView platformText;
TextView arrivalTimeText;
TextView statusText;
TextView destinationText;
TextView destinationTimeText;

public CustomViewHolder(View itemView) {

platformText =
arrivalTimeText =
statusText =
destinationText =
destinationTimeText =


First Read Document of TextView.

your arrival time is int and TextView have two type of setText method first is TextView.setText(String text); and second is TextView.setText(int resource);

when you write viewHolder.arrivalTimeText.setText(currentTrain.GetArrivalTime()); then you second type of method will call. int that case you have four value 1,2,3,4

means it's like viewHolder.arrivalTimeText.setText(1); at this time R.java may not have resource of value "1"; that's why you got android.content.res.Resources$NotFoundException: String resource ID #0x1

change viewHolder.arrivalTimeText.setText(currentTrain.GetArrivalTime()); line with