在 activity 的对象的帮助下在 RecyclerAdapter class 中访问 FragmentManager 时出现 IllegalStateException

IllegalStateException while acessing FragmentManager inside RecyclerAdapter class with the help of activity's object

我正在尝试通过单击按钮从我的 Recycler 适配器打开一个对话框。 为此,我需要使用 getFragmentManager 方法获取 FragmentManager,该方法只能在 activity 对象的帮助下调用。

这是我的代码:

回收器适配器 2:

   package com.example.batrad.expenseassist;

    import android.app.Activity;
    import android.app.FragmentManager;
    import android.content.Context;
    import android.media.Image;
    import android.support.v7.widget.RecyclerView;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ImageView;
    import android.widget.TextView;
    import android.widget.Toast;

    import java.util.Collections;
    import java.util.List;

    /**
     * Created by batrad on 11/18/2016.
     */

public class RecyclerAdapter2 extends RecyclerView.Adapter<RecyclerAdapter2.MyViewHolder> {
    List<Information2> data = Collections.emptyList();
    private LayoutInflater inflator;
    Activity activity;
    FragmentManager fm;

    public RecyclerAdapter2(Context context, List<Information2> data) {
        activity=(Activity)context;
        inflator = LayoutInflater.from(context);
        this.data = data;
         fm=activity.getFragmentManager();


    }

    @Override
    public RecyclerAdapter2.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view =inflator.inflate(R.layout.recycler_row_category,parent,false);
        MyViewHolder holder=new MyViewHolder(view);
        return holder;

    }

    @Override
    public void onBindViewHolder(RecyclerAdapter2.MyViewHolder holder, int position) {
        Log.d("tag",data.get(0)+"");
        Information2 info=data.get(position);
        Log.d("testre",info.categoryName+info.amount);
        holder.categoryName.setText(info.categoryName);
        holder.amount.setText(info.amount+"");
        holder.categoryImage.setImageResource(info.categoryImage);

    }

    public void onInsert(int size){
        notifyItemInserted(size);

    }



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

    class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        TextView categoryName;
        TextView amount;
        ImageView categoryImage;
        ImageView deleteIcon;
        ImageView addIcon;

        public MyViewHolder(View itemView) {
            super(itemView);
            categoryName=(TextView) itemView.findViewById(R.id.categoryName);
            amount=(TextView) itemView.findViewById(R.id.amount);
            categoryImage=(ImageView) itemView.findViewById(R.id.categoryImage);
            deleteIcon=(ImageView) itemView.findViewById(R.id.deleteIcon);
            addIcon=(ImageView) itemView.findViewById(R.id.addIcon);
            addIcon.setOnClickListener(this);

        }

        @Override
        public void onClick(View view) {

            SubCategoryDialog subCategoryDialog=new SubCategoryDialog();
            SubCategoryDialog.setValues(data.get(getAdapterPosition()).categoryName);
            subCategoryDialog.show(fm, "Create Category");


        }
    }
}

在 RecyclerAdapter2 的构造函数中,我正在从我的 MainActivity Java 文件传递​​ activity 的上下文:

MainActivity.java :

 categoryrecycleAdapter = new RecyclerAdapter2(this, getdata());

在 RecyclerAdapter2 构造函数内部,我将上下文分配给 Activity class 的对象。

然后我使用该对象来获取 fragmentManager。 但是我在这里得到 IllegalStateException :

java.lang.IllegalStateException: Activity has been destroyed
                                                                                      at android.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1433)
                                                                                      at android.app.BackStackRecord.commitInternal(BackStackRecord.java:687)
                                                                                      at android.app.BackStackRecord.commit(BackStackRecord.java:663)
                                                                                      at android.app.DialogFragment.show(DialogFragment.java:230)
                                                                                      at com.example.batrad.expenseassist.RecyclerAdapter2$MyViewHolder.onClick(RecyclerAdapter2.java:85)
                                                                                      at android.view.View.performClick(View.java:5233)
                                                                                      at android.view.View$PerformClick.run(View.java:21209)
                                                                                      at android.os.Handler.handleCallback(Handler.java:739)
                                                                                      at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                      at android.os.Looper.loop(Looper.java:152)

请帮忙。

这不是附加侦听器的正确方法。您应该在 Viewholder 的构造函数中设置 onClickListener,并使用如下所示的接口向您的片段发送回调,或者您也可以使用 this or this.

中的 BaseAdapter
    public interface OnItemClickListener {
        void onItemClick(View v);
    }

    private final OnItemClickListener listener;

    public RecAdapter(OnItemClickListener listener) {
        this.listener = listener;
    }

    public ViewHolder(View itemView) {
        super(itemView);
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onItemCLick(v);
            }
        });
    }