FragmentTransaction不能应用于&QUOT

FragmentTransaction不能应用于&QUOT

本文介绍了FragmentActivity切换错误..."不兼容的类型"或者QUOT; FragmentTransaction不能应用于"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从4.x到2.3.x.降级我的接口
4.x的接口设计采用片段和在工作。要降级了,我把它们改成FragmentActivties,交换一切所需的Andr​​oid的支持V4版本。
问题是,片段开关不能正常工作。

I needed to downgrade my Interface from 4.x to 2.3.x.The 4.x Interface was designed with Fragments and was working. To downgrade it, I changed them to FragmentActivties, switched everything to the needed android Support v4 version.The problem is, the Fragment switch is not working.

进口有:

import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;

错误code是在 ft.replace(..),基本上它说我需要一个片段出现,而不是MealsFragment。

Error Code is the ft.replace(..), basically it says I need a Fragment there, not the MealsFragment.

   @Override
   public void onClick(DialogInterface dialog, int which) {

            MealsFragment newFragment = null;
            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            if (which == 0) {
                Log.e("CLick", "CLICKED");
                newFragment = newMealsFragment();
                MealsFragment.meals = 1;

            } else if (which == 1) {
                changeFragment1 = new MeasurementFragment();
                MeasurementFragment.dia = 1;
            }

            ft.replace(R.id.fl_content_frame, newFragment);
            ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
            ft.commit();
        }

    });

错误AndroidStudio:

replace(int, android.support.v4.app.Fragment) in FragmentTransition cannot be applied to (int, xx.xx.xx.ui.MealsFragment)

MealsFragment是FragmentActivty

MealsFragment is an FragmentActivty

logcat的:

error: no suitable method found for replace(int,MealsFragment)
method FragmentTransaction.replace(int,Fragment,String) is not applicable
(actual and formal argument lists differ in length)
method FragmentTransaction.replace(int,Fragment) is not applicable
(actual argument MealsFragment cannot be converted to Fragment by method invocation conversion)

如果我的newFragment改为

If i change the newFragment to

android.support.v4.app.Fragment newFragment = null;

新的错误的不兼容的类型 occures。我在这两个错误之间的切换,但我不能得到解决。

The new error incompatible types occures. I'm switching between those two errors, but I can't get a solution.

推荐答案

您需要导入

import android.support.v4.app.Fragment

MealsFragment 。您需要使用片段从支持库,因为要扩展 FragmentActivity 这是支持基地的基类片段

in MealsFragment. You need to use fragment from the support library since you are extending FragmentActivity which is the base class for Support bases Fragments

这篇关于FragmentActivity切换错误..."不兼容的类型"或者QUOT; FragmentTransaction不能应用于"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 02:38