when replacing one fragment with another gives me error in the new fragment to insert

0

I have an activity that contains a fragment and a button outside of that fragment, I need to create an event that, by clicking on the button, replaces this fragment with another one. Here is a piece of code for you to see, the fragment " Fragment_Temas " gives me an error.

    two.setOnClickListener( new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            FragmentManager fragment_manager = getFragmentManager();
            FragmentTransaction transaction = fragment_manager.beginTransaction();
            transaction.replace( R.id.layout_fragment, Fragment_Temas);
            transaction.commit();
        }
    } );
    
asked by vidal_jr 11.10.2018 в 15:36
source

1 answer

0

You must instantiate the fragment to be able to replace the other fragment.

Try this:

two.setOnClickListener( new View.OnClickListener() {
   @Override
   public void onClick(View v) {

      Fragment_Temas fragmentTemas = new Fragment_Temas();

      FragmentManager fragment_manager = getFragmentManager();
      FragmentTransaction transaction = fragment_manager.beginTransaction();
      transaction.replace( R.id.layout_fragment, fragmentTemas);
      transaction.commit();
   }
});
    
answered by 11.10.2018 / 16:48
source