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); } }