I am getting null object reference error whiile passing data to one fragment to another

Pramesh Singh :

I am sending an integer value from one fragment to another fragment but I am getting null object reference error while executing. Below I am posting my two fragment classes and one ViewModel class where I am passing the data.

I am using this supporting for sending data to another fragments

RowViewModel.Class
         {
                    public BindableString albumName = new BindableString();
                    public BindableString albumDate = new BindableString();
                    public BindableString albumYear = new BindableString();
                    public BindableString albumMonth = new BindableString();
                    int position;
                    HappyMomentFragment happyMomentFragment;
                    ArrayList<RowHappyMomentViewModel> arrayList;
                    public static String TAG = "RowHappyMomentViewModel";

                  public RowHappyMomentViewModel(HappyMomentFragment happyMomentFragment, in position) {
                   this.happyMomentFragment = happyMomentFragment;
                   this.position = position;
                               }

                      public void onRowClick(View view) {
                      Bundle bundle=new Bundle();
                      bundle.putInt("Position", position);
                      happyMomentFragment.setArguments(bundle);
                      HappyMomentDetailFragment.addFragment((BaseActivity) 
                      happyMomentFragment.getActivity());
                      //Log.e(TAG, "position " + position);
    }
}

From this class, I am sending the data.

HappyMomentFragment.class                                         
  {
        HappyMomentViewModel mViewModel;
        FragmentHappyMomentBinding binding;
        @Override
        public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mViewModel = new HappyMomentViewModel(this);
    }
   @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_happy_moment, container, false);
        binding.setVm(mViewModel);
        ((BaseActivity) getActivity()).setToolbarVisibility(false);
        return binding.getRoot();

    }

    public static void addFragment(BaseActivity activity) {
        activity.replaceFragment(new HappyMomentFragment(), true);
    }

 **I am receiving the data in this class**




HapppyMomentDetailFragment.class{

    HappyMomentDetailViewModel mViewModel;
    FragmentHappyMomentDetailBinding binding;
    public final String TAG = "HappyMomentDetailFragment";
    int position;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mViewModel = new HappyMomentDetailViewModel(this);
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_happy_moment_detail, container, false);
        binding.setVm(mViewModel);
        Bundle args=getArguments();
        position = args.getInt("Position");
        ViewPager viewPager = binding.getRoot().findViewById(R.id.view_pager);
        TabLayout tabLayout = binding.getRoot().findViewById(R.id.tab_layout);
        tabLayout.setupWithViewPager(viewPager);
        ((BaseActivity) getActivity()).setToolbarVisibility(false);
        Log.e(TAG, "position " + position);
        return binding.getRoot();
    }

    public static void addFragment(BaseActivity activity) {
        activity.replaceFragment(new HappyMomentDetailFragment(), true);
    }

}
Marcos Vasconcelos :
public static void addFragment(BaseActivity activity) {
    activity.replaceFragment(new HappyMomentFragment(), true);
}

Here you are ignoring the previously created fragment and adding a new one without arguments you should actually change to this

public static void addFragment(BaseActivity activity, Fragment fragment) {
    activity.replaceFragment(fragment, true);
}

And you call you use the instance you have created:

                  happyMomentFragment.setArguments(bundle);
                  HappyMomentDetailFragment.addFragment((BaseActivity) 
                  happyMomentFragment.getActivity(), happyMomentFragment);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=174745&siteId=1