1

in my app i need to switch between fragments when different items on the navigation drawer are clicked. I created a new method DisplayFragment for it. Here is the code:

private void DisplayFragment(int position)
{
    Fragment fragment = null;
    switch (position){

        case 0:
            fragment = new Fragment1();
            break;

        case 1:
            fragment = new Fragment2();
            break;

        case 2:
            fragment = new Fragment3();
            break;

        case 3:
            fragment = new Fragment4();
            break;

    if(fragment!= null)
       this.getFragmentManager().beginTransaction().replace(R.id.frame_container,fragment()).commit();

}

it shows an error for "fragment element in above line as follows " wrong second argument type found android.support.v4.app.Fragment; required android.app.Fragment;"

i tried changing the import from android.support.v4.app.Fragment to android.app.Fragment; but it then shows an error for Fragment fragment = null statement. what am i doing wrong?

Rishit Shah
  • 355
  • 5
  • 20
  • `new fragment()` should be with capital letter `new Fragment()` and the import above should be `android.support.v4.app.Fragment` in all of your Fragment classes – Gabriella Angelova Jun 30 '15 at 13:09
  • Replace the code `.replace(R.id.frame_container, new fragment())` with `.replace(R.id.frame_container, fragment)`. You've already called "new Fragment()" in the switch statement above, now you need to assign that to be the new fragment. – Joseph Roque Jun 30 '15 at 13:14
  • possible duplicate of [Android replace the current fragment with another fragment](http://stackoverflow.com/questions/8163104/android-replace-the-current-fragment-with-another-fragment) – Jared Burrows Jun 30 '15 at 14:13
  • If you've found my answer helpful, please accept it. – C0D3LIC1OU5 Jul 21 '15 at 18:40

1 Answers1

0

You need to get the support fragment manager to use android.support.v4.app.Fragment.

this.getSupportFragmentManager().beginTransaction().replace(R.id.frame_container, fragment).commit();

so, getSupportFragmentManager() instead of getFragmentManager(). Also, you don't need to use this in this case.

C0D3LIC1OU5
  • 8,600
  • 2
  • 37
  • 47
  • i Tried that but it shows unreachable statement this.getSupportFragmentManager().beginTransaction().replace(R.id.frame_container,fragment).commit(); – Rishit Shah Jun 30 '15 at 17:51
  • Are you using Fragment Activity? http://developer.android.com/reference/android/support/v4/app/FragmentActivity.html – C0D3LIC1OU5 Jun 30 '15 at 17:53
  • yes I am.However my problem got solved when i put the above statement in each and every switch case. Will that make it work differently? – Rishit Shah Jun 30 '15 at 18:27
  • Please see the line in my answer above, I think you are using `new Fragment()` in your line, which means your switch statement doesn't work. Also, I am sure that this line won't compile, so I am not sure what you're actually using. `this.getFragmentManager().beginTransaction().replace(R.id.frame_container,new fragment()).commit();` There isn't such a thing as `new fragment()` – C0D3LIC1OU5 Jun 30 '15 at 18:31