28

I am trying to build a UI combining both static and dynamic elements. For this, I have divided my activity into fragments - all app navigation is then done by replacing fragments instead of navigating between activities.

In my main activity layout, I am using a FrameLayout:

<FrameLayout
        android:id="@+id/mainframe"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:layout_below="@id/topsection"
        android:layout_above="@id/lowersection" />

I have a fragment declared as such:

public class MyFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragmentlayout, container, false);
    }
}

Then, in my main activity (which extends FragmentActivity and uses the import android.support.v4.app.FragmentActivity, I am attempting to load this fragment into the frame layout.

MyFragment myf = new MyFragment();

FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.add(R.id.mainframe, myf);
transaction.commit();

I have followed this from many other examples, however I am receiving a compiler error on the transaction.add() command, which nobody else seems to have encountered.

The error I am receiving is: The method add(int, Fragment) in the type FragmentTransaction is not applicable for the arguments (int, MyFragment).

Why is this? The MyFragment class extends Fragment so I would've thought this would work. What am I doing wrong?

Edit: The imports for my main activity are:

import org.joda.time.DateTime;
import android.app.FragmentTransaction;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
Mike Baxter
  • 6,868
  • 17
  • 67
  • 115
  • Are you extending FragmentActivity in your main activity class? – wyoskibum Dec 08 '13 at 16:10
  • 1
    sometimes we do mistake when importing two same class from two different location while doing keyboard shortcut i would suggest you to cross check your imported classes since you are not using support library as i thought previously. Nevertheless it would be helpful for us to find out the issue if you can put your imported classes here. – saiful103a Dec 08 '13 at 16:12
  • @wyoskibum my main activity does extend `FragmentActivity`, not `Activity`. – Mike Baxter Dec 08 '13 at 16:15
  • @saiful103a I am using the import `android.support.v4.app.FragmentActivity`. That is the only option that eclipse prompted me to import - is there a different one that I should be using? – Mike Baxter Dec 08 '13 at 16:15
  • @Teifi when you use support library you should not use any of the native fragment component, since you are using support library use all the fragment related component form support library. – saiful103a Dec 08 '13 at 16:20
  • 1
    import FragmentTransaction from support library android.support.v4.app.FragmentTransaction; also since you are using support library use getSupportFragmentManager() instead of getFragmentManager() – saiful103a Dec 08 '13 at 16:22

1 Answers1

32

Check your imports. Use android.support.v4.app.FragmentTransaction instead of android.app.FragmentTransaction.

Furthermore be sure you are using android.support.v4.app.Fragment and calling getSupportFragmentManager(). It's easy to miss this calls / imports. Thx to saiful103a with the hint of the FragmentManager.

Steve Benett
  • 12,843
  • 7
  • 59
  • 79