Error incompatible types: android.app.FragmentManager cannot be converted to android.support.v4.app.FragmentManager

Tony picture Tony · Nov 20, 2015 · Viewed 19.6k times · Source

Error Error incompatible types: android.app.FragmentManager cannot be converted to android.support.v4.app.FragmentManager is drving me nuts.

In my app, I have 3 navigation drawer item, timesheet,claim and view, created in MainActivity. Now I wanted to add two tabs with swipe view in view item.

MainActivity // for navigation drawer

   import android.app.Fragment;
   import android.app.FragmentManager;
   private void selectItem(int position) {

            Fragment fragment = null;

            switch (position) {
                case 0:
                    fragment=new TimeSheet();
                    break;
                case 1:

                    fragment=new Claims1();
                    break;

                case 2:
                    fragment=new Viewview();
                    break;

                default:
                    break;
            }

TabsFragmentPagerAdapter.java

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

public class TabsFragmentPagerAdapter extends FragmentPagerAdapter {

    public TabsFragmentPagerAdapter(FragmentManager fm) {
        super(fm);
        // TODO Auto-generated constructor stub
    }

    @Override
    public Fragment getItem(int index) {
        // TODO Auto-generated method stub

        switch(index) {
            case 0:
                return new UpdatePage2();
            case 1:
                return new Receipt();

        }
        return null;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return 2;
    }

}

ViewView.java

import android.app.Fragment;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.app.ActionBar;
import android.support.v4.widget.SimpleCursorAdapter;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.example.project.project.database.MyDatabaseHelper;

public class ViewView extends Fragment implements ActionBar.TabListener {

    InfoAPI sqlcon;
    private SimpleCursorAdapter dataAdapter;
    private SQLiteDatabase database;
    private MyDatabaseHelper dbHelper;
    private ListView listView;
    private ViewPager viewPager;
    private TabsFragmentPagerAdapter mAdapter;
    private ActionBar actionBar;
    private String[] tabs = {"Information", "receipt"};



    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View View1 = inflater.inflate(R.layout.viewview1, container, false);
        listView = (ListView) View1.findViewById(R.id.listView1);
        dbHelper = new MyDatabaseHelper(getActivity());
        sqlcon = new InfoAPI(getActivity());
        viewPager = (ViewPager) View1.findViewById(R.id.pager);
        mAdapter = new TabsFragmentPagerAdapter(getActivity().getFragmentManager()); //here the error
        viewPager.setAdapter(mAdapter);
        actionBar = getActivity().getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        for (int i = 0; i < 2; i++) {
            actionBar.addTab(actionBar.newTab().setText(tabs[i]).setTabListener(this));
        }
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int arg) {
                // TODO Auto-generated method stub
                actionBar.setSelectedNavigationItem(arg);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
                // TODO Auto-generated method stub

            }
        });

        BuildList();
        return View1;
    }

Problem :

Error:(49, 81) error: incompatible types: android.app.FragmentManager cannot be converted to android.support.v4.app.FragmentManager

If changed import android.support.v4.app.FragmentManager; in TabsFragmentPagerAdapter to import android.app.FragmentManager;, the first issue solve but get this Error:(12, 15) error: incompatible types: android.app.FragmentManager cannot be converted to android.support.v4.app.FragmentManager...I have no idea how to fix ..

Edited

MainActivity

import android.app.Fragment;
import android.support.v4.app.FragmentManager;
  case 2:
                    fragment=new ViewView();
                    break;

                default:
                    break;
            }

            if (fragment != null) {
                FragmentManager fragmentManager = getFragmentManager();
                fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

                mDrawerList.setItemChecked(position, true);
                mDrawerList.setSelection(position);
                setTitle(mNavigationDrawerItemTitles[position]);
                mDrawerLayout.closeDrawer(mDrawerList);

            } else {
                Log.e("MainActivity", "Error in creating fragment");
            }
        }

Error

Error:(148, 30) error: incompatible types: ViewView cannot be converted to Fragment Error:(156, 69) error: incompatible types: android.app.FragmentManager cannot be converted to android.support.v4.app.FragmentManager Error:(157, 80) error: incompatible types: android.app.Fragment cannot be converted to android.support.v4.app.Fragment

Answer

Sanket Shah picture Sanket Shah · Nov 20, 2015

Change

import android.app.Fragment;
import android.app.FragmentManager;

to

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

in every class. you are facing problems because in your Fragment creation class you are using support v4 fragment and in your MainActivity class you are inflating as a simple fragment.

You also need to change getFragmentManager() to getSupportFragmentManager(), and make sure they're extending a FragmentActivity class.

Hope it will help you.