当我从RecyclerView的适配器的setOnClickListener启动活动时,应用程序崩溃

时间:2016-11-30 08:08:53

标签: android android-recyclerview adapter

我有一个 RecyclerView ,它正在加载我的String Arrays的内容,但是我想打开一个新的 Activity ,具体取决于哪个视图他们已经按下了。

我做了什么:

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

我已经阅读了许多文章来解决它并按照这样做了但找不到任何解决方案。

1 个答案:

答案 0 :(得分:0)

实际上,我无法回答您的问题,因为您没有发布您的logcat,但您可以检查的内容很少:

1 - 检查您的活动是否已在AndroidManifest.xml中声明

2 - 检查上下文是Activity或applicationContext的上下文? 如果不是Activity的上下文,那么你需要添加标志

Intent i = new Intent(this, WinnerActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);