Cannot convert from android.support.v4.app.Fragment to android.app.Fragment

I'm doing my first Android app, and wanted to get straight into the ICS API. I have so far created an app using an ActionBar, with swipeable tabs using Viewpager and Fragments.

I do however experience some errors that I keep returning to.

Depending on how I implement it, it always keep going back to an "Type mismatch" error: "cannot convert from android.support.v4.app.Fragment to android.app.Fragment". I have tried removing all import references to either, and this error appears when I only use android.support.v4.app.Fragment in TabListener, FragmentActivity and my two Fragments.

The error occurs in my TabListener:

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.util.Log;

public class TabListener implements ActionBar.TabListener {
    private android.app.Fragment fragment;
    private Activity activity;
    private ViewPager pager;
    private FragmentTransaction ft;

    public TabListener(Activity activity, Fragment fragment, ViewPager pager) {
        this.activity = activity;
        this.fragment = fragment;
        this.pager = pager;
    }

    @Override
    public void onTabSelected(Tab tab, android.app.FragmentTransaction ft){     
        if (fragment == null) {
            ft.add(fragment, null);
        } else {
            ft.attach(fragment);
        }
    }

    @Override
    public void onTabReselected(Tab tab, android.app.FragmentTransaction ft){
        // TODO Auto-generated method stub
    }

    @Override
    public void onTabUnselected(Tab tab, android.app.FragmentTransaction ft){
        // TODO Auto-generated method stub  
    }
}

By removing "android.app.FragmentTransaction ft", replacing it with just "FragmentTransaction ft", the problem goes awawy. Then new problems arise:

The method onTabReselected(ActionBar.Tab, FragmentTransaction) of type TabListener must override or implement a supertype method TabListener.java

The method onTabSelected(ActionBar.Tab, FragmentTransaction) of type TabListener must override or implement a supertype method TabListener.java

The method onTabUnselected(ActionBar.Tab, FragmentTransaction) of type TabListener must override or implement a supertype method TabListener.java

The type TabListener must implement the inherited abstract method ActionBar.TabListener.onTabReselected(ActionBar.Tab, FragmentTransaction) TabListener.java

The type TabListener must implement the inherited abstract method ActionBar.TabListener.onTabSelected(ActionBar.Tab, FragmentTransaction) TabListener.java

The type TabListener must implement the inherited abstract method ActionBar.TabListener.onTabUnselected(ActionBar.Tab, FragmentTransaction) TabListener.java

Whats going on here?

As you may understand, I'm new to Java and Android development. I feel like I'm pretty close, but I'm not able to solve this problem. I don't understand why it want to "convert from android.support.v4.app.Fragment to android.app.Fragment when I'm not even importing android.app.Fragment anywhere.

I guess it's related to using the compatibility package. (Do I have to use this package at all when creating an app for the newest version of the SDK?)

Try to use getSupportFragmentManager() instead getFragmentManager()

cannot convert from android.app.FragmentManager to android , I am using Xamarin and am recoding a Google Maps application to use Android.​Support.V4.App.Fragments with a View Pager. Here is my  As you may understand, I'm new to Java and Android development. I feel like I'm pretty close, but I'm not able to solve this problem. I don't understand why it want to "convert from android.support.v4.app.Fragment to android.app.Fragment when I'm not even importing android.app.Fragment anywhere. I guess it's related to using the compatibility

Whats going on here?

While the Android Support package gives you a backwards-compatible Fragment implementation, the ActionBar is not part of the Android Support package. Hence, ActionBar.TabListener is expecting native API Level 11 Fragment objects. Consider using ActionBarSherlock to have both an action bar and Android Support fragments.

but then I'm left with another problem in my FragmentPagerAdapter

The FragmentPagerAdapter in the Android Support package is a bit messy -- it wants API Level 11 Fragment objects, not Android Support Fragment objects. However, you can clone the source to FragmentPagerAdapter (source is in your SDK) and create your own implementation that uses the support.v4 flavor of Fragment and kin.

Cannot convert type 'Android.Support.V4.App.Fragment' to 'Android , android.support.v4.app. When using the <fragment> tag, this implementation can not use the parent view's ID as From class android.app. If you use the v4 compatibility package, your Activity has to extend FragmentActivity, not Activity. If you use Android > 3.0, then your Activity extends Activity, but read CommonsWare's answer about not mixing both types of fragments.

I know that it has been too late to answer this question but it might help someone with the same problem.

Go to your java folder and click on your fragment's activity.

In the imports, replace import android.app.Fragment; with

import android.support.v4.app.Fragment;

Keep the code in the MainActivity intact and this should help resolve the issue.

Note: If it doesn't work at once, don't worry. Build > Rebuild project.

FragmentActivity, I'm doing my first Android app, and wanted to get straight into the ICS API. I have so far created an app using an ActionBar, with swipeable tabs  cannot convert to Ti convert-to cannot convert param Cannot convert value android.support.v4 Cannot return from o Cannot cast from Vie Cannot load from mysql.proc char convert to int Convert a Number to VB convert to VC Convert Mysql to Oracle cannot connect to ho convert convert Convert Cannot android.support.v4.app.Fragment c++ The convert from int to char or char to int From Frequency to

This solution works for me

replace

public class MyFragment extends Fragment{
}

with

public class MyFragment extends android.support.v4.app.Fragment{
}

and also replace import

import android.app.Fragment;

with

import android.support.v4.app.Fragment;

Fragment, FragmentTransaction; import android.support.v7.app. android.app.​FragmentTransaction cannot be converted to android.support.v4.app. Static library support version of the framework's Fragment.Used to write apps that run on platforms prior to Android 3.0. When running on Android 3.0 or above, this implementation is still used; it does not try to switch to the framework's implementation.

You can remove the support package, and that should solve your problem. It is only needed when you need functions from Android 3.0 and above in apps for earlier versions. In your case you get both the default Fragments from ICS, and the Fragments from the support package, and if you happen to get objects from the different packages they will not work together.

Short version; You use either an api level above Honecomb or the support package, not both.

Getting error in FragmentTransaction fragmentTransaction , Android-Cannot convert type 'Android.Support.V4.App.Fragment' to Android-​The type android.support.v4.app.Fragment cannot be resolved. Xamarin Android​  Static library support version of the framework's ListFragment.Used to write apps that run on platforms prior to Android 3.0. When running on Android 3.0 or above, this implementation is still used; it does not try to switch to the framework's implementation.

android.support.v4.app.fragment cannot be resolved, FragmentManager; import android.support.v4.app. FavoriteActivity(); //ERROR: Type mismatch: cannot convert from FavoriteActivity to Fragment case 1: return  java: cannot access android.support.v4.app.Fragment class file for android.support.v4.app.Fragment not found. please help me i have to do the facebook integration to my application within a day please respond immediately.

Start an activity from a fragment, Static library support version of the framework's android.app.DialogFragment. Static library support version of the framework's android.app.Fragment. Base class for activities that want to use the support-based Fragments. FragmentContainerView is a customized Layout designed specifically for Fragments. Provides integration points with a

Comments
  • I try this TutViewerFragment viewer = (TutViewerFragment)getSupportFragmentManager().findFragmentById(R.id.tutview_fragment); but shows error "cannot cast from fragment to TutView Fragment"
  • what is parent of TutViewerFragment?
  • its extends from FragmentActivity
  • @gayavat, why when I use getSupportFragmentManager(); Android Studio says undefined!
  • may be it helps stackoverflow.com/questions/13212353/…
  • Cloning the FragmentPagerAdapter, and asking it to use android.app.Fragment/import android.app.FragmentManager/import android.app.FragmentTransaction seems to have worked.. No errors this time! Thanks
  • SLekvak, does your implementation work on api 11 and lower devices, or just on api 13+?
  • I understand. I have tried removing the need to use the support package, but then I'm left with another problem in my FragmentPagerAdapter - which I can't import as "android.app.FragmentPagerAdapter". >The return type is incompatible with FragmentPagerAdapter.getItem(int) MainFragmentActivity.java It tries here to return fragment, which is Fragment mFragment = LatestFragment.newInstance(position);
  • It is perfectly reasonable to have a build target of API Level 11 or higher and use the Android Support package.
  • @CommonsWare: I was under the impression that the support package (v4) was built on features from API level 11 (the v13 package slipped my mind), but in op's case, when he develops for ICS (API>13), the contents of the packages should be supported without additional packages, or am I wrong?
  • @Jave: Well, I can't quite follow everything the OP wrote. :-) If you are only deploying to API Level 11 and higher, you can use the native Fragment implementation supplied by API Level 11. If you are deploying to older devices, you need to use the Android Support package, regardless of your build target. It's entirely possible to want an ICS build target for selective things (e.g., CalendarContract) that can be bypassed on older devices, yet still want to use the Android Support package for fundamental stuff that needs to be fully backwards-compatible.
  • @CommonsWare: Then I am with you there, My assumption was that OP was targeting and deploying on API level >13.