gpt4 book ai didi

android - FragmentActivity 切换错误... "Incompatible types"或 "FragmentTransaction cannot be applied"

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:39:42 24 4
gpt4 key购买 nike

我需要将我的界面从 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

日志:

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 中。您需要使用支持库中的 fragment ,因为您正在扩展 FragmentActivity,它是 Support bases Fragments 的基类

关于android - FragmentActivity 切换错误... "Incompatible types"或 "FragmentTransaction cannot be applied",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22195836/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com