Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ListFragment Support Library GetSupportFragmentManager undefined

I have a list Fragment (importing the v4 support library, build target is 2.3.3 api 11) and the following problem in my code that was working fine when the target was 3.0 and I didn't use the library.

Fragment frag1 = new ExampleFragment();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.contentframe, frag1);
ft.commit();

Now I'm getting the compile error that

"Type mismatch: cannot convert from ExampleFragment to Fragment"

and it tells me to change the "Fragment" to ExampleFragment (e.g.)

ExamplesFragment frag1 = new ExamplesFragment();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.contentframe, frag1);
ft.commit();

Then I have a problem that the ft.replace command has an error:

The method replace(int, Fragment) in the type FragmentTransaction is not applicable for the arguments (int, Fragment_PVAnn)

and it tells me to change the ExamplesFragment in the first line back to just Fragment and so the circle goes round and around

like image 285
Killerpixler Avatar asked Jun 26 '12 13:06

Killerpixler


2 Answers

You should use

getActivity().getSupportFragmentManager()

If that is still giving you problems, then makre sure that your Activity extends FragmentActivity.

If that is still giving you problems,

  1. Delete all of your imports,
  2. Re-import with Ctrl + Shift + O, and
  3. On every name clash, make sure you import from support.v4.*, not android.*.
like image 145
Alex Lockwood Avatar answered Nov 08 '22 02:11

Alex Lockwood


You have to make sure that when you extend Fragment that you are extending the Fragment that is in the v4 package space. Check your imports for Fragment, FragmentActivity, etc, and if you see them imported WITHOUT .v4 in the package name, then it is wrong.

Also, if you set the android library for the project to be 2.3 and not 4.0.x then you'll quickly see if you are using the incorrect classes, because you'll get compile errors, which can be fixed by using the v4 classes from the android support jar (which hopefully you've included in your project as well)

like image 40
stuckless Avatar answered Nov 08 '22 03:11

stuckless