0

In my first attempt to work with Fragments, the method replace gives this error The method replace(int, Fragment) in the type FragmentTransaction is not applicable for the arguments (int, LM_Fragment) I supplied the id of the first fragment which is lm_fragment as the first parameter for .replace method. is that correct? if not what parameter should I supply .replace method with?

MainActivity:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Configuration config = getResources().getConfiguration();

      FragmentManager fragmentManager = getFragmentManager();
      FragmentTransaction fragmentTransaction = 
      fragmentManager.beginTransaction();

      if (config.orientation == Configuration.ORIENTATION_LANDSCAPE) {
         LM_Fragment lm_fragment = new LM_Fragment();
         fragmentTransaction.replace(R.id.lm_fragment, lm_fragment);
      }else{
         PM_Fragment pm_fragment = new PM_Fragment();
         fragmentTransaction.replace(R.id.pm_fragment, pm_fragment);
      }
      fragmentTransaction.commit();
   }

MainActivity xml file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">

     <fragment
        android:name="com.example.fragments"
        android:id="@+id/lm_fragment"
        android:layout_weight="1"
        android:layout_width="0dp"
       android:layout_height="match_parent" />

    <fragment
        android:name="com.example.fragments"
        android:id="@+id/pm_fragment"
        android:layout_weight="2"
        android:layout_width="0dp"
        android:layout_height="match_parent" />

Amrmsmb
  • 1
  • 27
  • 104
  • 226
  • Does your LM_Fragment class inhert from android's Fragment class? – wastl May 26 '14 at 11:02
  • yes "public class LM_Fragment extends Fragment" – Amrmsmb May 26 '14 at 11:05
  • You need to pass `containerViewId` as First argument of your `.replace(....)` – M D May 26 '14 at 11:07
  • Take a look on this [http://notionink.wikidot.com/rajeshbabu](http://notionink.wikidot.com/rajeshbabu) And [http://stackoverflow.com/questions/5658675/replacing-a-fragment-with-another-fragment-inside-activity-group](http://stackoverflow.com/questions/5658675/replacing-a-fragment-with-another-fragment-inside-activity-group) – M D May 26 '14 at 11:11
  • is `containerViewId ` the same as ` – Amrmsmb May 26 '14 at 11:12

1 Answers1

0

Your LM_Fragment should be inheriting the Fragment class. Also in replace(int, fragment) you should pass container id. Container is the view which will hold the fragment, its not the fragment id.

Manish
  • 1,215
  • 10
  • 29