Manish Tiwari Manish Tiwari - 1 year ago 71
Android Question

Application creashed when i want to start activity from within setOnClick Listener of Recycler View adapter

I've got a RecyclerView which is loading the content of my String arrays which works fine, however I want to open a new activity depending on which view they have pressed.

What I have done:

public class WinnersListAdapter extends RecyclerView.Adapter<WinnersListAdapter.WinnersCardViewHolder>
Context context;
LayoutInflater inflater;
List<ListLottery> lotteryList;

public WinnersListAdapter(List<ListLottery> loteries,Context context){
this.context = context;
this.lotteryList = loteries;
inflater = LayoutInflater.from(context);

public WinnersListAdapter.WinnersCardViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = inflater.inflate(R.layout.prevlotterycards_layout,parent,false);

// Toast.makeText(context,"Hi",Toast.LENGTH_SHORT).show();
WinnersListAdapter.WinnersCardViewHolder viewHolder = new WinnersListAdapter.WinnersCardViewHolder(v);
return viewHolder;

public void onBindViewHolder(WinnersListAdapter.WinnersCardViewHolder holder, int position) {
final ListLottery lottery = lotteryList.get(position);
holder.itemView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(context,WinnerActivity.class);

public int getItemCount() {
return lotteryList.size();

class WinnersCardViewHolder extends RecyclerView.ViewHolder {
TextView lottery_date;
CardView card_lotteryList;

public WinnersCardViewHolder(View itemView) {
lottery_date = (TextView) itemView.findViewById(;
card_lotteryList = (CardView) itemView.findViewById(;

I have read many articles to resolve it and did stuff according that but could not find any solution.

Answer Source

Actually, I can not answer your question because you didn't post your logcat but there are few things you can to check :

1 - Check that your activity already declared in AndroidManifest.xml

2 - Check you context is the context of Activity or applicationContext ? If is't not the context of Activity so you need to add flag

Intent i = new Intent(this, WinnerActivity.class);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download