0

I have CustomDialog class that extends DialogFragment. I want to get ArrayList from my Adapter. I Have this Exception java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.Serializable android.os.Bundle.getSerializable(java.lang.String)' on a null object reference when I try to run.

holder.choisirButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        int positionselected = menuItem.getId();
        Bundle bundle = new Bundle();
        bundle.putSerializable("menuItemList", menuItemList);
        bundle.putInt("positionselected", positionselected);
        FragmentActivity activity = (FragmentActivity) (context);
        FragmentManager fm = activity.getFragmentManager();
        ComplementFragment alertDialog = new ComplementFragment();
        alertDialog.show(fm, "fragment_alert");
    }
});

public class ComplementFragment extends DialogFragment {

    ArrayList<MenuItem> menuItemsList;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.custom_dialog_layout, container);

        menuItemList = (ArrayList<MenuItem>) getArguments().getSerializable("menuItemList");
        int selectedPosition = getArguments().getInt("positionselected");
        MenuItem menuItem = menuItemList.get(selectedPosition);
        return view;
    }
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

1 Answers1

0

You many need to cast serializable while putting the array in bundle. You can check the answers from here. OR you can use a method to create custom dialog and receive the array as parameter in that method. Check the code from here. Although the best way of solving it is using parceable. Details about using parcelable can be found from here and here. For using parcelable to pass data to fragments, check this answer. I hope my answer is helpful.

Imtiaz Abir
  • 731
  • 7
  • 12