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

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

问题描述

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

在我的应用程序中,我在 MainActivity 中创建了 3 个导航抽屉项,timesheetclaimview.现在我想在 view 项中添加两个 tabsswipe view.

MainActivity//用于导航抽屉

 导入android.app.Fragment;导入android.app.FragmentManager;私人无效selectItem(int位置){片段片段 = null;开关(位置){案例0:片段=新的时间表();休息;情况1:片段=新索赔1();休息;案例2:片段=新视图视图();休息;默认:休息;}

TabsFragmentPagerAdapter.java

导入android.support.v4.app.Fragment;导入 android.support.v4.app.FragmentManager;导入 android.support.v4.app.FragmentPagerAdapter;公共类 TabsFragmentPagerAdapter 扩展 FragmentPagerAdapter {公共 TabsFragmentPagerAdapter(FragmentManager fm) {超级(调频);//TODO 自动生成的构造函数存根}@覆盖公共片段getItem(int index){//TODO 自动生成的方法存根开关(索引){案例0:返回新的 UpdatePage2();情况1:返回新收据();}返回空值;}@覆盖公共 int getCount() {//TODO 自动生成的方法存根返回 2;}}

ViewView.java

导入android.app.Fragment;导入 android.content.DialogInterface;导入android.content.Intent;导入android.database.sqlite.SQLiteDatabase;导入android.os.Bundle;导入android.app.FragmentTransaction;导入 android.support.v4.view.ViewPager;导入android.app.ActionBar;导入 android.support.v4.widget.SimpleCursorAdapter;导入 android.support.v7.app.AlertDialog;导入 android.view.LayoutInflater;导入 android.view.Menu;导入android.view.View;导入android.view.ViewGroup;导入 android.widget.AdapterView;导入 android.widget.ListView;导入 com.example.project.project.database.MyDatabaseHelper;公共类 ViewView 扩展片段实现 ActionBar.TabListener {InfoAPI sqlcon;私有 SimpleCursorAdapter 数据适配器;私有 SQLiteDatabase 数据库;私有 MyDatabaseHelper dbHelper;私有 ListView 列表视图;私人 ViewPager viewPager;私有 TabsFragmentPagerAdapter mAdapter;私有ActionBar actionBar;private String[] tabs = {"信息", "收据"};public View onCreateView(LayoutInflater inflater, ViewGroup 容器, Bundle savedInstanceState) {super.onCreate(savedInstanceState);查看 View1 = inflater.inflate(R.layout.viewview1, 容器, 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());//这里是错误viewPager.setAdapter(mAdapter);actionBar = getActivity().getActionBar();actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);对于 (int i = 0; i < 2; i++) {actionBar.addTab(actionBar.newTab().setText(tabs[i]).setTabListener(this));}viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {@覆盖公共无效 onPageSelected(int arg){//TODO 自动生成的方法存根actionBar.setSelectedNavigationItem(arg);}@覆盖公共无效 onPageScrolled(int arg0,float arg1,int arg2){//TODO 自动生成的方法存根}@覆盖公共无效 onPageScrollStateChanged(int arg0){//TODO 自动生成的方法存根}});构建列表();返回视图1;}

问题:

<块引用>

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

如果将TabsFragmentPagerAdapter中的import android.support.v4.app.FragmentManager;改为import android.app.FragmentManager;,第一个问题解决但得到这个 Error:(12, 15) error: incompatible types: android.app.FragmentManager cannot be convert to android.support.v4.app.FragmentManager...我不知道怎么做修复..

编辑

MainActivity

导入android.app.Fragment;导入 android.support.v4.app.FragmentManager;案例2:片段=新视图视图();休息;默认:休息;}如果(片段!= null){FragmentManager fragmentManager = getFragmentManager();fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();mDrawerList.setItemChecked(位置,真);mDrawerList.setSelection(位置);setTitle(mNavigationDrawerItemTitles[位置]);mDrawerLayout.closeDrawer(mDrawerList);} 别的 {Log.e("MainActivity", "创建片段时出错");}}

错误

<块引用>

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

解决方案

改变

导入android.app.Fragment;导入android.app.FragmentManager;

导入android.support.v4.app.Fragment;导入 android.support.v4.app.FragmentManager;

在每个班级.您正面临问题,因为在您的 Fragment 创建类中您使用支持 v4 片段,而在您的 MainActivity 类中您正在膨胀为一个简单的片段.

您还需要将 getFragmentManager() 更改为 getSupportFragmentManager(),并确保它们扩展了 FragmentActivity 类.p>

希望对你有所帮助.

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

解决方案

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.

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

上一篇:Android TabWidget空白问题 下一篇:Viewpager 标签片段 &amp;正常片段

相关文章

最新文章