错误不兼容类型:android.app.FragmentManager无法转换为android.support.v4.app.FragmentManager

时间:2015-11-20 08:55:19

标签: java android tabs navigation-drawer swipeview

错误Error incompatible types: android.app.FragmentManager cannot be converted to android.support.v4.app.FragmentManager让我疯了。

在我的应用中,我在MainActivity中创建了3个导航抽屉项timesheetclaimview。现在,我想在tabs项中添加两个swipe viewview

MainActivity //导航抽屉

   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;
    }

问题:

  

错误:(49,81)错误:不兼容的类型:android.app.FragmentManager   无法转换为android.support.v4.app.FragmentManager

如果import android.support.v4.app.FragmentManager;中的TabsFragmentPagerAdapter更改为import android.app.FragmentManager;,则第一个问题会解决,但会得到Error:(12, 15) error: incompatible types: android.app.FragmentManager cannot be converted to android.support.v4.app.FragmentManager ...我不知道如何解决..

被修改

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");
            }
        }

错误

  

错误:(148,30)错误:不兼容的类型:ViewView不能   转换为片段错误:(156,69)错误:不兼容的类型:   android.app.FragmentManager无法转换为   android.support.v4.app.FragmentManager错误:(157,80)错误:   不兼容的类型:android.app.Fragment无法转换为   android.support.v4.app.Fragment

1 个答案:

答案 0 :(得分:18)

更改

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

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

在每个班级。您遇到问题,因为在 Fragment 创建类中,您使用的是支持v4片段,而在 MainActivity 类中,您正在使用支持v4片段。

您还需要将getFragmentManager()更改为getSupportFragmentManager(),并确保他们扩展FragmentActivity课程。

希望它会对你有所帮助。