There are two buttons; by pressing one, a fragment with a red background is added, and the other with a blue background. When you call the add (FragmentTransaction) method, the whole bracket with the parameters is underlined in red. What is the problem? Maybe it's in the libraries?

import android.app.Activity; import android.app.FragmentManager; import android.app.FragmentTransaction; import android.os.Bundle; import android.view.View; public class MainActivity extends Activity { private FragmentManager mFragmentManager; private FragmentTransaction mFragmentTransaction; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mFragmentManager = getFragmentManager(); } public void onClick(View view) { mFragmentTransaction = mFragmentManager.beginTransaction(); switch (view.getId()){ case R.id.buttonBlue: BlueFragment blueFragment = new BlueFragment(); mFragmentTransaction.add(R.id.container, blueFragment);//в этих местах ошибка break; case R.id.buttonRed: RedFragment redFragment = new RedFragment(); mFragmentTransaction.add(R.id.container, redFragment);//в этих местах ошибка break; default: mFragmentTransaction.commit(); } } 

}

Blue code:

 public class BlueFragment extends Fragment { public BlueFragment() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_blue, container, false); } } 
  • I did not understand the sentence "When calling a method ... the whole bracket with parameters is underlined in red." So you have a code compilation or execution error? - Eugene Krivenja
  • Something tells me that your fragments are not fragments at all, give the code of one of them. - Eugene Krivenja
  • Examine work with fragments, in particular transactions. Because your transaction code is incorrect in principle. developer.android.com/guide/components/fragments.html - Eugene Krivenja
  • @EugeneKrivenja Added code. Error when compiling and executing, although the code from the tutorial site developer.alexanderklimov.ru/android/theory/fragment-add.php . - IvanOdintsov
  • What does the IDE write when you hover the mouse on the underlined red? and your fragments will not be displayed anyway, because the commit will be only on the default branch when no buttons are pressed. You need to know more about the principle of the switch-case operator - pavlofff

2 answers 2

In the activation, you use the fragments (and auxiliary classes) from the android.app package, and the fragment class itself is in your android.support.v4.app.Fragment package. Because of this conflict.

In the fragment class, replace the import:

 //import android.support.v4.app.Fragment; - этот импорт удалить import android.app.Fragment; // - этот импорт добавить public class BlueFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_blue, container, false); } } 

or vice versa, in activize, use classes to work with fragments from the android.support.v4 package. The default constructor in the fragment is also not needed if there are no other constructors in it.

PS: In order for the switch-case selector to actually cause fragments, the commit of the fragment must be unconditional, and not the default option of the selector, since there it will not be called when you click on any of the buttons, but only when you click on any other button except these two - it is unlikely that you thought of just such an algorithm:

 public void onClick(View view) { mFragmentTransaction = mFragmentManager.beginTransaction(); switch (view.getId()){ case R.id.buttonBlue: BlueFragment blueFragment = new BlueFragment(); mFragmentTransaction.add(R.id.container, blueFragment); break; case R.id.buttonRed: RedFragment redFragment = new RedFragment(); mFragmentTransaction.add(R.id.container, redFragment); break; } mFragmentTransaction.commit(); } 
  • Yes, thank you very much, the problem is solved - IvanOdintsov

Try this:

  import android.support.v4.app.FragmentTransaction; public class MainActivity extends Activity { private FragmentManager mFragmentManager; private FragmentTransaction mFragmentTransaction; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mFragmentManager = getFragmentManager(); } public void onClick(View view) { mFragmentTransaction = getSupportFragmentManager().beginTransaction(); switch (view.getId()){ case R.id.buttonBlue: BlueFragment blueFragment = new BlueFragment(); mFragmentTransaction .replace( R.id.container, blueFragment); break; case R.id.buttonRed: RedFragment redFragment = new RedFragment(); mFragmentTransaction .replace( R.id.container, redFragment); break; default: mFragmentTransaction.commit(); } } }