我需要将接口从4.x降级到2.3.x. 4.x接口是使用Fragments设计的,并且正在运行.要降级它,我将它们更改为FragmentActivties,将所有内容切换到所需的android Support v4版本.问题是,Fragment交换机无法正常工作.
进口是:
import android.support.v4.app.FragmentTransaction; import android.support.v4.app.FragmentManager; import android.support.v4.app.Fragment; import android.support.v7.app.ActionBarActivity;
错误代码是ft.replace(..),基本上它说我需要一个Fragment,而不是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
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更改为
android.support.v4.app.Fragment newFragment = null;
发生新的错误不兼容类型.我在这两个错误之间切换,但我无法得到解决方案.
你需要导入
import android.support.v4.app.Fragment
在MealsFragment
.您需要使用支持库中的片段,因为您正在扩展FragmentActivity
,这是支持库片段的基类