Q:

How to call a custom dialog fragment from a RecyclerViewAdapter inside a fragment in Android

I am trying to call a custom dialog fragment i have created from a dialog fragment adapter that extends a recyclerView.  I'm using the latest android studio with androidx.  the following code is for the Custom dialog Adapter
import android.app. DialogFragment;
import android.os. Bundle;
import android.view. LayoutInflater;
import android.view. View;
import android.view. ViewGroup;

import tino.varconn.com.instantecocash. R;

public class DialogSendMoneyFirst extends DialogFragment {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_send_money_first, container, false);

    // Do all the stuff to initialize your custom view

    return v;
}

The following code i am trying to use to call the dialog fragment
package tino.varconn.com.instantecocash;

import android.content. Context;
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 androidx.appcompat.app. AppCompatActivity;
import androidx.cardview.widget. CardView;
import androidx.fragment.app. FragmentTransaction;
import androidx.recyclerview.widget. RecyclerView;

import java.util. List;

import tino.varconn.com.instantecocash.dialogs. DialogSendMoneyFirst;

public class EcocashAdapter extends 
RecyclerView. Adapter<EcocashAdapter. MyViewHolder> {

private List<EcocashModel> itemList;
Context context;

public class MyViewHolder extends RecyclerView. ViewHolder {
    public TextView title;
    public ImageView img;
    public CardView cardView;

    public MyViewHolder(View view) {
        super(view);
        title = (TextView) view.findViewById(R.id.listtext);
        img = (ImageView) view.findViewById(R.id.logo);
        cardView = (CardView) view.findViewById(R.id.cardView);
    }
}

public EcocashAdapter(List<EcocashModel> itemList) {
    this.itemList = itemList;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.my_list_row, parent, false);

    return new MyViewHolder(itemView);
}

@Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
    EcocashModel ecocashModel = itemList.get(position);
    holder.title.setText(ecocashModel.getName());
    holder.img.setImageResource(ecocashModel.getImage());

    holder.itemView.setOnClickListener(new View. OnClickListener() {
        @Override
        public void onClick(View v) {
            if (position == 0){
                Toast.makeText(v.getContext(), "Send Money", Toast. LENGTH_SHORT).show();
                DialogSendMoneyFirst dialog = new DialogSendMoneyFirst();
                dialog.show(((AppCompatActivity)context).getFragmentManager(), "MyCustomDialog");

            }
            if (position == 1){
                Toast.makeText(v.getContext(), "Pay Merchant", Toast. LENGTH_SHORT).show();
            }
        }
    });

}

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

And this is the error :
Attempt to invoke virtual method 'android.app. FragmentManagerandroidx.appcompat.app. AppCompatActivity.getFragmentManager()' on a null object reference

A:

Try this
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.my_list_row, parent, false);
    context = itemView.getContext()
    return new MyViewHolder(itemView);
}

If not work : 
1.Check your activity is AppCompact or not. 
2.If you are calling your adapter from fragment then use getChildFragmentManager()

If you are calling your adapter from Activity then use getSupportFragmentManager()

