37

I'm getting an error in this part of code:

private void displayView(int position) {
    // update the main content by replacing fragments
    Fragment fragment = null;
    switch (position) {
        case 0:
            fragment = new HomeFragment();
            break;
        case 1:
            fragment =new FindPeopleFragment();
            break;
        case 2:
            fragment = new PhotosFragment();
            break;
        case 3:
            fragment = new CommunityFragment();
            break;
        case 4:
            fragment = new PagesFragment();
            break;
        case 5:
            fragment = new WhatsHotFragment();
            break;

        default:
            break;
    }

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

        // update selected item and title, then close the drawer
        mDrawerList.setItemChecked(position, true);
        mDrawerList.setSelection(position);
        setTitle(navMenuTitles[position]);
        mDrawerLayout.closeDrawer(mDrawerList);
    } else {
        // error in creating fragment
        Log.e("MainActivity", "Error in creating fragment");
    }
}

I get

error: incompatible types: HomeFragment cannot be converted to Fragment

this is the imports:

package liorsiag.lgbt;
import android.app.FragmentManager;
import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import java.util.ArrayList;

and this is the class title:

public class MainActivity extends FragmentActivity {
    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    private ActionBarDrawerToggle mDrawerToggle;

No matter what I've tried I still get this error

I've tried a lot of navigation drawer tutorials, but none of them seem to work.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Lior
  • 393
  • 1
  • 3
  • 8

10 Answers10

78

This seems to be an import problem.

When using getFragmentMangager(), make sure that your Fragment classes extend android.app.Fragment class.

If by any chance you are using android.support.v4.app.Fragment (see your imports), then you need to use getSupportFragmentManager() instead

Hope it helps

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
zozelfelfo
  • 3,776
  • 2
  • 21
  • 35
  • 1
    Thank you. But I still have a problem now with this line: `FragmentManager fragmentManager = getSupportFragmentManager();` It Says `Cannot resolve method`. – Lior Nov 20 '14 at 11:19
  • 3
    `getSupportFragmentManager()` works if your `Activity` is a `FragmentActivity` if that is not the case, use `getFragmentManager()` – zozelfelfo Nov 20 '14 at 11:27
  • Tried, yet this line: `fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).commit();` does this problem: `Error:(191, 52) error: incompatible types: android.support.v4.app.Fragment cannot be converted to android.app.Fragment`. – Lior Nov 20 '14 at 11:36
  • Make sure that `fragment` object is an `android.support.v4.app.Fragment` and not an `android.app.Fragment` – zozelfelfo Nov 20 '14 at 12:12
  • Made sure, didn't solve the problem... It says the problem is what shows the fragments in the activity_main.xml. Any help? – Lior Nov 20 '14 at 12:46
  • Change this import `import android.app.FragmentManager;`to `import android.support.v4.app.FragmentManager;` – zozelfelfo Nov 20 '14 at 13:21
  • Changed it but it says it can't resolve `getSupportFragmentManager()` in `FragmentManager fragmentManager = getSupportFragmentManager();`. – Lior Nov 20 '14 at 14:24
  • @zozelfelfo can you please help me to solve this https://stackoverflow.com/questions/50046175/fragment-cannot-be-converted-to-context-in-android – Nikson Apr 26 '18 at 14:59
10

Try changing

import android.app.Fragment;

to

import android.support.v4.app.Fragment;

Use classes from that support lib for all other imports too. Also getSupportFragmentManager() as mentioned in the other answer.

ramuta
  • 260
  • 2
  • 11
4

In your HomeFragment class

replace:

import android.app.Fragment;

with:

import android.support.v4.app.Fragment;
GhostCat
  • 137,827
  • 25
  • 176
  • 248
3

In my case i have changed line-1 with line-2

Line-1: import android.app.Fragment;

Line-2: import android.support.v4.app.Fragment;

Its working

Burhanuddin Rashid
  • 5,260
  • 6
  • 34
  • 51
Harunduet
  • 937
  • 10
  • 11
2

use getSupportFragmentManager() Instead of getFragmentManager()

getSupportFragmentManager()
    .beginTransaction()
    .replace(in.jama.app.R.id.container, new Fragment())
    .commit();
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
saigopi.me
  • 14,011
  • 2
  • 83
  • 54
2

you just have to import android.support.v4.app.Fragment; in the all the FragmentClass();. that's it.

Mehul Raj
  • 21
  • 3
1

In Android Studio 2.3 getSupportFragmentManager works with android.support.v4.app but android studio 3.1 you have to use getFragmentManager enter image description here

Faizan Haidar Khan
  • 1,099
  • 1
  • 15
  • 20
0

import android.app.Fragment; works with getFragmentManager() method but before you have to remove the import android.support.v4.app.Fragment;

Robert
  • 5,278
  • 43
  • 65
  • 115
Negatu
  • 1
0

If you are using support library, you should ensure to import both Fragment and FragmentManager from the support library. You will also need to ensure to load the support fragment manager.

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
//other imports here...

public class SomeActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_some);

        FragmentManager fm = getSupportFragmentManager();
        Fragment fragment = fm.findFragmentById(R.id.fragment_container);

        if(fragment == null) {
            fragment = new SomeFragment();
            fm.beginTransaction().add(R.id.fragment_container, fragment).commit();
        }

    }
}
Shripada
  • 6,296
  • 1
  • 30
  • 30
0

i having same issue so i updated

implementation 'androidx.appcompat:appcompat:1.2.0' 

to

implementation 'androidx.appcompat:appcompat:1.3.0-beta01'

it is working fine now

BugsCreator
  • 433
  • 3
  • 10