Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fragment add or replace not working

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; 
like image 725
EGHDK Avatar asked Jul 23 '12 19:07

EGHDK


People also ask

How to replace fragment in android studio?

Use replace() to replace an existing fragment in a container with an instance of a new fragment class that you provide. Calling replace() is equivalent to calling remove() with a fragment in a container and adding a new fragment to that same container. transaction.

How to use FragmentManager in android?

To display a fragment within a layout container, use the FragmentManager to create a FragmentTransaction . Within the transaction, you can then perform an add() or replace() operation on the container. . commit();


2 Answers

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.

like image 168
Cat Avatar answered Oct 23 '22 00:10

Cat


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!!

like image 30
whyoz Avatar answered Oct 22 '22 23:10

whyoz