Fragment add or replace does not work

I am using the code from the link.

When I paste this code into my program, I get an error message as shown in the figure 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; 
+54
java android android-fragments
Jul 23 '12 at 19:58
source share
5 answers

The problem is that you are mixing android.support.v4.app.Fragment and android.app.Fragment . You need to convert all the support libraries used to use it, 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 Fragment support library and normal Fragment NOT interchangeable. They achieve the same goal, but cannot be replaced by each other in code.

+164
Jul 23 2018-12-12T00:
source share

Despite the fact that this question may have been answered, it should be noted that the solution for overlapping fragments is to get the fragment identifier (in fact, the FrameLayout identifier as an ad in your xml will lead to headaches) with a fresh copy " Fragment":

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

I can’t say how many hours I spent post after posting without a decision. I read your other post related in the comments above, and I'm going to reply there also in case someone finds this first.

For those who get a ClassCastException , try this. You can add all the libraries you need using FragmentActivity instead of Fragment , and getActivity (). GetSupportFragmentManager in your code to stop errors in the ListFragment, and you still run into problems with fragments. Google Docs don't show you everything, and completing the Eclipse code will not always save you ... sometimes you just need to fix the error yourself.

+6
May 08 '13 at 22:34
source share

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

stack overflow

0
May 08 '15 at
source share

Thisworked for me

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

0
Jul 19 '17 at 13:45
source share

Try

 FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); fragmentTransaction.replace(R.id.frag, new ExampleFragments()).commit(); 
0
May 6 '19 at 11:10
source share



All Articles