FragmentActivity切换错误..."不兼容的类型"或"FragmentTransaction无法应用"

 妖姬脸似花甘露_545 发布于 2023-01-19 19:55

我需要将接口从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;

发生新的错误不兼容类型.我在这两个错误之间切换,但我无法得到解决方案.

1 个回答
  • 你需要导入

    import android.support.v4.app.Fragment
    

    MealsFragment.您需要使用支持库中的片段,因为您正在扩展FragmentActivity,这是支持库片段的基类

    2023-01-19 19:58 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有