Search code examples
androidandroid-fragmentsfragmentmanager

Fragmentmanager issue


I am trying to have a fragment appear when a button is pressed on the template navigation drawer. I named my fragment "BluetoothFragment". Here is the code that I am having issues with:

@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.nav_bluetooth) {
        BluetoothFragment bluetoothFragment = new BluetoothFragment();
        FragmentManager manager = getSupportFragmentManager();
        manager.beginTransaction().replace(R.id.ConstraintLayoutforfragment, bluetoothFragment).commit();

    } else if (id == R.id.nav_file) {

    } else if (id == R.id.nav_watch_import) {

    } else if (id == R.id.nav_account_settings_variant) {

    } else if (id == R.id.nav_share) {

    } else if (id == R.id.nav_send) {

    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

The line that has the error is:

manager.beginTransaction().replace(R.id.ConstraintLayoutforfragment, bluetoothFragment).commit();

Where the "BluetoothFragment" gives the error:

"Wrong 2nd argument type. Found: com.example.tferfilemanager.BluetoothFragment, required: android.support.v4.app.Fragment

Cheers for any help and advice


Solution

  • Try this:

    Fragment fragment = new BluetoothFragment();
    

    if it doesn't work also, check if BluetoothFragmentextends android.support.v4.app.Fragment