Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 585
broesel001 Avatar asked Dec 06 '22 03:12

broesel001


2 Answers

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

like image 169
Raghunandan Avatar answered Dec 10 '22 12:12

Raghunandan


use this

import android.support.v4.app.Fragment;

in MealsFragment extends Fragment as well

like image 25
vipul mittal Avatar answered Dec 10 '22 11:12

vipul mittal