FragmentActivity switching error... "Incompatible types" or "FragmentTransaction cannot be applied"

12,277

Solution 1

You need to import

import android.support.v4.app.Fragment

in MealsFragment. You need to use fragment from the support library since you are extending FragmentActivity which is the base class for Support bases Fragments

Solution 2

use this

import android.support.v4.app.Fragment;

in MealsFragment extends Fragment as well

Share:
12,277
broesel001
Author by

broesel001

Updated on June 17, 2022

Comments

  • broesel001
    broesel001 about 2 years

    I needed to downgrade my Interface from 4.x to 2.3.x. The 4.x Interface was designed with Fragments and was working. To downgrade it, I changed them to FragmentActivties, switched everything to the needed android Support v4 version. The problem is, the Fragment switch is not working.

    Imports are:

    import android.support.v4.app.FragmentTransaction;
    import android.support.v4.app.FragmentManager;
    import android.support.v4.app.Fragment;
    import android.support.v7.app.ActionBarActivity;
    

    Error Code is the ft.replace(..), basically it says I need a Fragment there, not the MealsFragment.

       @Override
       public void onClick(DialogInterface dialog, int which) {
    
                MealsFragment newFragment = null;
                FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                if (which == 0) {
                    Log.e("CLick", "CLICKED");
                    newFragment = newMealsFragment();
                    MealsFragment.meals = 1;
    
                } else if (which == 1) {
                    changeFragment1 = new MeasurementFragment();
                    MeasurementFragment.dia = 1;
                }
    
                ft.replace(R.id.fl_content_frame, newFragment);
                ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
                ft.commit();
            }
    
        });
    

    Error in AndroidStudio:

    replace(int, android.support.v4.app.Fragment) in FragmentTransition cannot be applied to (int, xx.xx.xx.ui.MealsFragment)
    

    MealsFragment is an FragmentActivty

    Logcat:

    error: no suitable method found for replace(int,MealsFragment)
    method FragmentTransaction.replace(int,Fragment,String) is not applicable
    (actual and formal argument lists differ in length)
    method FragmentTransaction.replace(int,Fragment) is not applicable
    (actual argument MealsFragment cannot be converted to Fragment by method invocation conversion)
    

    If i change the newFragment to

    android.support.v4.app.Fragment newFragment = null;
    

    The new error incompatible types occures. I'm switching between those two errors, but I can't get a solution.

  • broesel001
    broesel001 over 10 years
    Thanks for the advice, followed a guidline, where it said that I need to change every Fragment to an FragmentActivity. It's working now thanks a lot