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

这是我的代码:

RecyclerAdapter2:

   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文件传递了活动的上下文:

MainActivity.java:

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


在RecyclerAdapter2构造函数中,我将上下文分配给Activity类的object。

然后我使用该对象来获取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)


请帮忙 。

最佳答案

这不是附加侦听器的正确方法。您应该在查看器的构造函数中设置onClickListener,并应使用如下所示的接口向片段发送回调,也可以从thisthis使用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);
            }
        });
    }

07-28 02:22