58

I'm using the code from this reference.

When I put in that code in my program I get an error as seen in the picture below. enter image description here

Any reasons for the error? The method replace(int, Fragment) in the type FragmentTransaction is not applicable for the arguments (int, ExampleFragments)

Code from my main activity:

public void red(View view) {
        android.app.FragmentManager fragmentManager = getFragmentManager();
                android.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        ExampleFragments fragment = new ExampleFragments();
        fragmentTransaction.replace(R.id.frag, fragment);
        fragmentTransaction.commit();
    }

ExampleFragments.java

package com.example.learn.fragments;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

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

Here:

package com.example.learn.fragments;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
EGHDK
  • 17,818
  • 45
  • 129
  • 204
  • Can you show your `ExampleFragments` class? I suspect it does not extend `Fragment`. – Cat Jul 23 '12 at 20:00
  • Updated my questions to show ExampleFragments.java – EGHDK Jul 23 '12 at 20:03
  • Why are you using `ExampleFragments fragment = new ExampleFragments();` and not `Fragment fragment = new Fragment()` ? – mattdonders Jul 23 '12 at 20:03
  • I copied and pasted from http://developer.android.com/guide/components/fragments.html `You can then add a fragment using the add() method, specifying the fragment to add and the view in which to insert it. For example:` – EGHDK Jul 23 '12 at 20:05
  • 2
    just to clarify, in order to get rid of the errors, you have to make Android's sample FragmentLayout.java code's "DetailsActivity" extend "FragmentActivity" not "Activity," then Eric's solution will work. – whyoz Apr 24 '13 at 21:20
  • You need to check the imports. import the version of Fragment which is needed in the replace() method. I was also facing same in Android Studio and its solved now. – Rahul Raina Jan 13 '16 at 10:30

5 Answers5

167

The problem here is that you're mixing android.support.v4.app.Fragment and android.app.Fragment. You need to convert all uses to use the support library, which also means calling getSupportFragmentManager().

Something like this, for example:

    android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
    android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    ExampleFragments fragment = new ExampleFragments();
    fragmentTransaction.replace(R.id.frag, fragment);
    fragmentTransaction.commit();

It is important to note that the support library Fragment and the normal Fragment are NOT interchangeable. They achieve the same purpose, but they cannot be replaced with one another in code.

Cat
  • 66,919
  • 24
  • 133
  • 141
  • Getting an error on `getSupportFragmentManager()` The method getSupportFragmentManager() is undefined for the type MainActivity. I'm updating my question to show imports. – EGHDK Jul 23 '12 at 20:08
  • 12
    Your activity needs to extend FragmentActivity, not Activity. – Sam Dozor Jul 23 '12 at 20:32
  • @Sam_D - Beat me to it. EGHDK - There is an explanation of that [here](http://stackoverflow.com/questions/5209170/android-compatibility-package-doesnt-include-activity-getfragmentmanager). – Cat Jul 23 '12 at 20:33
  • So if the activity deals with fragments at all it "has" to extend fragment activity? – EGHDK Jul 23 '12 at 20:44
  • 2
    Yes, some form of `FragmentActivity`. There are extended classes, such as `ListFragmentActivity`, or `SherlockFragmentActivity` (if using ActionBarSherlock), but yes, all `Activity`s handling `Fragments` must extend some form of a `FragmentActivity`. – Cat Jul 23 '12 at 20:45
  • Awesome. Very insightful. I have one last question though, because the code works, but adds the layout and doesn't replace it. IE: The old layout in the fragment is still there. Should replace have taken care of that? – EGHDK Jul 23 '12 at 20:53
  • Yes, it should have. However, I would suggest marking a solution for this question and asking a new question for that, as sharing code gets to be rather cumbersome in comment streams. You may also find your solution in other `Fragment` or `.replace()` threads. – Cat Jul 23 '12 at 21:05
  • Thanks. I will start another question because I feel like I am executing the solution to my problem the wrong way. Thanks – EGHDK Jul 23 '12 at 21:11
  • Here is my new question. Thanks in advance: http://stackoverflow.com/questions/11620855/replacing-fragments-isnt-working-am-i-executing-this-the-proper-way – EGHDK Jul 23 '12 at 21:33
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/14307/discussion-between-eghdk-and-eric) – EGHDK Jul 23 '12 at 21:49
  • Ensure that ExampleFragments class also imports android.support.v4.app.Fragment; and not android.support.app.Fragment; – stackoverflow Nov 17 '13 at 23:56
  • Eric help me.I am getting a weird problem.`https://stackoverflow.com/questions/25443583/comiple-errorthe-method-replace-in-the-type-fragmenttransaction-is-not-applicab`.please check out and tell me the suggestion. – Stephen Aug 22 '14 at 13:58
  • Thank you Eric. I spent a week on this issue. This works for me. – Sepaka Mar 17 '15 at 11:11
  • Hi initial fragment replacement is working but while second time replacing same fragment getting null pointer error please help me thanks in advance – vijay Jan 11 '16 at 07:23
8

Although this question may have been answered, it should be noted that the solution to overlapping fragments is to get the fragment ID (actually the FrameLayout id as declaring in your xml will lead to headaches) with a fresh "Fragment" instance:

FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment fragment = new ExampleFragments();
fragmentTransaction.replace(R.id.frag, fragment);
fragmentTransaction.commit();

I can't tell you how many hours I spent going through post after post with no solution. I read your other post that's linked in the comments above and I'm going to answer there as well just in case someone finds that first.

For those who are getting a ClassCastException, try this as well. You can have all the right libraries added, using FragmentActivity instead of Fragment, and have getActivity().getSupportFragmentManager in your code to stop errors in a ListFragment and you'll still run into problems with Fragments. Google docs don't show you everything, and Eclipse code completion will not always save you...sometimes you just have to fix the bug yourself!!

Al Lelopath
  • 6,448
  • 13
  • 82
  • 139
whyoz
  • 5,168
  • 47
  • 53
  • "(actually the FrameLayout id as declaring in your xml will lead to headaches) " saved me – vinay Dec 20 '18 at 16:15
0

No matter what I tried (including the ones written here), I could not solve this issue until I saw this answer

https://stackoverflow.com/a/5907704/728312

Community
  • 1
  • 1
Alpaslan
  • 233
  • 1
  • 2
  • 11
0

Thisworked for me

android.app.FragmentManager fragmentManager = getActivity().getFragmentManager();

Mudasar
  • 159
  • 1
  • 7
0

Try it

FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
  fragmentTransaction.replace(R.id.frag, new ExampleFragments()).commit();