Facing issue in calling fragment class from adapter class in android

2.4k Views Asked by At

I have to call a fragment class from baseAdapter class. I have created a method in main fragment class like this

public  void switchContent(Fragment fragment) {

    getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.frame_container, new Reply_ozoneFragment()).commit();

    }

and calling this method in adapter class like this.

holder.bt_reply.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {

        fragment.switchContent();

        FragmentManager fm = getFragmentManager();

                 FragmentTransaction fragmentTransaction = fm.beginTransaction();
           fragmentTransaction.replace(R.id.frame_container, new Reply_ozoneFragment());

                 fragmentTransaction.commit();


        /*Intent reintent = new Intent(context,Reply_ozoneFragment.class);

        //reintent.putExtra("userid", S);
        context.startActivity(reintent);
        */
    }
}); 

but this is not working properly and my app crashes every time. Please tell me what mistake I am doing here.

1

There are 1 best solutions below

0
On

remove fragment.switchContent(); from your adapter button onclick() method and change

FragmentManager fm = getFragmentManager();

to

FragmentManager fm = getActivity().getSupportFragmentManager()