【问题标题】:FragmentActivity switching error... "Incompatible types" or "FragmentTransaction cannot be applied"FragmentActivity 切换错误...“不兼容的类型”或“无法应用 FragmentTransaction”
【发布时间】: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


【解决方案1】:

你需要导入

import android.support.v4.app.Fragment

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

【讨论】:

  • 感谢您的建议,遵循了指导方针,其中说我需要将每个 Fragment 更改为 FragmentActivity。现在可以正常使用了,非常感谢
【解决方案2】:

使用这个

import android.support.v4.app.Fragment;

MealsFragment extends Fragment 中也是如此

【讨论】:

    猜你喜欢
    • 2015-06-13
    • 1970-01-01
    • 1970-01-01
    • 2021-04-27
    • 2017-10-03
    • 1970-01-01
    • 2018-05-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多