【发布时间】:2014-03-05 11:01:41
【问题描述】:
我需要将我的界面从 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;
出现新错误不兼容的类型。我正在这两个错误之间切换,但我找不到解决方案。
【问题讨论】:
-
片段类的导入是什么
标签: android android-fragments android-fragmentactivity