Error initializing a fragment

1

People give me this little mistake when initializing the Fragment. BlankFragment is my Fragment. says that the Fragment class is not compatible with the BlankFragment class extending this from Fragment

private void seleccionarItem(MenuItem itemDrawer) {
    Fragment fragmentoGenerico = null;
    android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();

    switch (itemDrawer.getItemId()) {
        case R.id.nav_camera:
            *********fragmentoGenerico = new BlankFragment();*******
            break;
        case R.id.nav_gallery:
            // Fragmento para la sección Cuenta


            break;
        case R.id.nav_manage:
            // Fragmento para la sección Categorías

            break;
        case R.id.nav_send:
            // Iniciar actividad de configuración

            break;
    }
    if (fragmentoGenerico != null) {
        fragmentManager
                .beginTransaction()
                .replace(R.id.contenedor_principal, fragmentoGenerico)
                .commit();
    }
    // Setear título actual
    setTitle(itemDrawer.getTitle());
    getSupportActionBar().setLogo(itemDrawer.getIcon());

}
    
asked by Erio Cedeno 11.11.2018 в 04:46
source

1 answer

0

You must ensure that Fragment use the import:

import android.support.v4.app.Fragment;
...
...
...
Fragment fragmentoGenerico = null;

and BlankFragment must extend from Fragment and also use the same import.

import android.support.v4.app.Fragment;
...
...
public class BlankFragment extends Fragment {

FragmentManager and FragmentTransaction must make use of the import

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

The problem is because you are probably using the wrong import in this case, which is:

import android.app.Fragment
    
answered by 11.11.2018 / 05:43
source