I have a RecyclerHolder called EventHolder which I use in multiple layouts. It works perfectly in one activity. In the second activity, while the RecyclerView displays everything correctly, it throws a null pointer exception when I click on one of the items in the list in an attempt to move to the 'ViewSingleEvent' activity. I cannot figure out why this is, especially since it works for the first layout.
Code from EventHolder class:
@Override
public void onClick(View v) {
//handle the on click event for the ViewHolder - get single event details and switch activity
if (this.event != null) {
//getting the ID of the particular event clicked
String indEventId = this.event.id;
//using an intent to pass data between values
Intent i = new Intent(context, ViewSingleEvent.class); //nullpointerexception here
i.putExtra("id",indEventId);
context.startActivity(i);
}
}
The line Intent i = new Intent(context, ViewSingleEvent.class);
holds the error. Is this something to do with getting the context of the second activity? Please help.
ALSO: I have already looked at multiple similar NullPointerException questions already, and have not found the answer to my question, so please don't mark this question as a duplicate - I'm stumped.
Edit: Code from EventHolder class where I get the context.
private Context context;
public EventHolder(final Context context, View itemView) {
super(itemView);
this.context = context;
I THINK this is how I pass the context from the first activity (the one that works):
//declaring the layout manager
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
eventsListview.setLayoutManager(layoutManager);
This is what I did for the one that doesn't work. Note that it is a fragment, not an activity.
//declaring the layout manager
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
eventsListview.setLayoutManager(layoutManager);