Reputation: 341
entering in code like this:
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
comes up with this error "android.support.v4.app.FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();" and has this as a quick fix which does in fact remove the error:
android.support.v4.app.FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
and i am typing this in my main activity which extends FragmentActivity . Does anybody know why? i have included:
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
EDIT
DescriptionFragment fragment = new DescriptionFragment();
android.support.v4.app.FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.pager, fragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.commit();
changing getSupportFragmentManager() to getFragmentManager() requires me to change DescriptionFragment to android.app.Fragment...any ideas?
Upvotes: 26
Views: 41698
Reputation: 111
In Activity import android.app.Fragment .. In Fragment extend with android.app.Fragment.. this solved my issue...
Upvotes: 1
Reputation: 159
getActivity() slove my issueandroid.app.FragmentManager fragmentManager = getActivity().getFragmentManager();
Upvotes: 1
Reputation: 5249
try this
public class DescriptionFragment extends android.support.v4.app.Fragment
instead of simply Fragment.
Upvotes: 3
Reputation: 2104
Ok...Just make a method for fragment and call this Like:
void addfragment(Fragment fragment, boolean addBacktoStack, int transition) {
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.simple_fragment, fragment);
ft.setTransition(transition);
if (addBacktoStack)
ft.addToBackStack(null);
ft.commit();
}
Now Just call Your Fragment:
addfragment(new DescriptionFragment(this),true, FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
Upvotes: 2
Reputation: 2104
Have you import android.support.v4.jar file to your project??
Upvotes: 2
Reputation: 696
With getSupportFragmentManager() you are getting the supportLibrary fragmentManager instead of the systems fragmentManager. So you are working with a transaction of the supportlibrary.
This is the reason why you need to add all these imports and use android.support.v4.app.
If you want to get the systems fragmentManager just try to use getFragmentManager() instead getSupportFragmentManager().
Hope this helps
Upvotes: 37