简体   繁体   中英

Application is crashing when I start activity from setOnClickListener of RecyclerView's 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);
    }

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

    @Override
    public void onBindViewHolder(WinnersListAdapter.WinnersCardViewHolder holder, int position) {
        final ListLottery lottery = lotteryList.get(position);
        holder.lottery_date.setText(lottery.getLotteryDate());
        holder.lottery_date.setTag(holder);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context,"clicked--"+lottery.getLotteryId(),Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(context,WinnerActivity.class);
                intent.putExtra("lotteryId",lottery.getLotteryId());
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return lotteryList.size();
    }

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

        public WinnersCardViewHolder(View itemView) {
            super(itemView);
            lottery_date = (TextView) itemView.findViewById(R.id.l_lottery_date);
            card_lotteryList = (CardView) itemView.findViewById(R.id.card_lotteryList);
        }
    }
}

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

Actually, I can not answer your question because you didn't post your logcat but there are few things you can 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);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM