错误不兼容类型:android.app.FragmentManager无法转换为android.support.v4.app.FragmentManager [英] Error incompatible types: android.app.FragmentManager cannot be converted to android.support.v4.app.FragmentManager

查看:5964
本文介绍了错误不兼容类型:android.app.FragmentManager无法转换为android.support.v4.app.FragmentManager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

错误错误不兼容类型:android.app.FragmentManager无法转换为android.support.v4.app.FragmentManager 让我疯了。

在我的应用中,我有3个导航抽屉项目,时间表声明 view 。现在我想在视图标签滑动视图 >项目。

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 //导航抽屉

   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

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

如果更改导入android.support.v4.app.FragmentManager; in TabsFragmentPagerAdapter import android.app.FragmentManager; ,第一个问题解决但得到这个错误:(12,15)错误:不兼容的类型:android.app.FragmentManager无法转换为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 ..

已修改

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

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


推荐答案

更改

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

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

。您遇到问题,因为在您的 Fragment 创建类中,您使用的是支持v4片段,而在 MainActivity 类中,您正在膨胀为一个简单的片段。

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.

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

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

希望它会对你有帮助。

这篇关于错误不兼容类型:android.app.FragmentManager无法转换为android.support.v4.app.FragmentManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆