无法添加窗口-令牌null不适用于Service

时间:2019-03-28 22:19:31

标签: java android android-recyclerview dialog

我有一个基于CardCard的RecyclerView的应用程序,我想在单击卡片视图时显示一个警告对话框,但是当我设置onClickListener并单击卡片视图时,该应用程序崩溃,并收到以下错误消息:无法添加窗口-令牌null不适用于Service中的应用程序。 (“进度对话框”也会发生同样的情况)。 相反,如果在onclick中设置了祝酒词,则不会收到任何错误。 这是代码:

holder.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder=new AlertDialog.Builder(context);
                builder.setTitle("Description");
                LinearLayout layout=new LinearLayout(context);
                TextView text=new TextView(context);
                layout.addView(text);
                builder.setView(layout);
                builder.setCancelable(true);
                AlertDialog alertDialog=builder.create();
                alertDialog.show();
            }
        });

这是完整类MyAdapter的代码:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    private List<Film> listItems;
    private Context context;


    public MyAdapter(List<Film> listItems, Context context) {
        this.listItems = listItems;
        this.context = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.view_layout, parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
        final Film listItem = listItems.get(position);
        holder.textViewName.setText(listItem.getName());
        holder.textViewCategory.setText(listItem.getCategory());
        holder.textViewYear.setText(""+listItem.getYear());
        holder.textViewDirection.setText(listItem.getDirection());
        Picasso.with(context).load(listItem.getUrl()).into(holder.imageView);


        holder.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder=new AlertDialog.Builder(context);
                builder.setTitle("Description");
                LinearLayout layout=new LinearLayout(context);
                TextView text=new TextView(context);
                layout.addView(text);
                builder.setView(layout);
                builder.setCancelable(true);
                AlertDialog alertDialog=builder.create();
                alertDialog.show();
            }
        });



    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {

        public TextView textViewName;
        public TextView textViewCategory;
        public TextView textViewYear;
        public TextView textViewDirection;
        public ImageView imageView;
        public CardView cardView;


        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            textViewName = (TextView) itemView.findViewById(R.id.name);
            textViewCategory = (TextView) itemView.findViewById(R.id.category);
            textViewYear = (TextView) itemView.findViewById(R.id.year);
            textViewDirection=(TextView) itemView.findViewById(R.id.direction);
            imageView = (ImageView) itemView.findViewById(R.id.playbill);
            cardView=(CardView) itemView.findViewById(R.id.cardView);

        }
    }
}

0 个答案:

没有答案