Gnemlock
Gnemlock

Reputation: 326

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 onBindViewHolder. 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 findViewById. 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:

MainActivity.java

public class MainActivity extends AppCompatActivity {

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

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

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

        mTrains = FillTrainData();
        mTrainAdapter = new TrainAdapter(this, mTrains);
        mRecyclerView.setAdapter(mTrainAdapter);
    }

    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]);
            mTempTrainData.add(mTrain);
        }

        return mTempTrainData;
    }
}

TranAdapter.java

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;
    }

    @Override
    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;
    }

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

        viewHolder.platformText.setText(currentTrain.GetPlatform());
        viewHolder.arrivalTimeText.setText(currentTrain.GetArrivalTime());
        viewHolder.statusText.setText(currentTrain.GetStatus());
        viewHolder.destinationText.setText(currentTrain.GetDestination());
        viewHolder.destinationTimeText.setText(currentTrain.GetDestinationTime());
    }

    @Override
    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) {
            super(itemView);

            platformText = 
                    (TextView)itemView.findViewById(R.id.train_item_platform_text);
            arrivalTimeText = 
                    (TextView)itemView.findViewById(R.id.train_item_arrival_time_text);
            statusText = 
                    (TextView)itemView.findViewById(R.id.train_item_status_text);
            destinationText = 
                    (TextView)itemView.findViewById(R.id.train_item_destination_text);
            destinationTimeText =
                    (TextView)itemView.findViewById(R.id.train_item_destination_time_text);
        }
    }
}

Upvotes: 4

Views: 2376

Answers (1)

prakash ubhadiya
prakash ubhadiya

Reputation: 1271

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

viewHolder.arrivalTimeText.setText(String.valueOf(currentTrain.GetArrivalTime()));

Upvotes: 19

Related Questions