无法从 android.support.v4.app.Fragment 转换为 android.app.Fragment

我正在开发我的第一个 Android 应用,并且想直接使用 ICS API.到目前为止,我已经使用 ActionBar 创建了一个应用程序,并带有使用 Viewpager 和 Fragments 的可滑动标签.

I'm doing my first Android app, and wanted to get straight into the ICS API. I have so far created an app using an ActionBar, with swipeable tabs using Viewpager and Fragments.

但是,我确实遇到了一些我不断返回的错误.

I do however experience some errors that I keep returning to.

根据我的实现方式,它总是会回到类型不匹配"的状态.错误:无法从 android.support.v4.app.Fragment 转换为 android.app.Fragment".我已经尝试删除对任何一个的所有导入引用,当我只在 TabListener、FragmentActivity 和我的两个 Fragment 中使用 android.support.v4.app.Fragment 时会出现此错误.

Depending on how I implement it, it always keep going back to an "Type mismatch" error: "cannot convert from android.support.v4.app.Fragment to android.app.Fragment". I have tried removing all import references to either, and this error appears when I only use android.support.v4.app.Fragment in TabListener, FragmentActivity and my two Fragments.

错误发生在我的 TabListener 中:

The error occurs in my TabListener:

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.util.Log;

public class TabListener implements ActionBar.TabListener {
    private android.app.Fragment fragment;
    private Activity activity;
    private ViewPager pager;
    private FragmentTransaction ft;

    public TabListener(Activity activity, Fragment fragment, ViewPager pager) {
        this.activity = activity;
        this.fragment = fragment;
        this.pager = pager;
    }

    @Override
    public void onTabSelected(Tab tab, android.app.FragmentTransaction ft){     
        if (fragment == null) {
            ft.add(fragment, null);
        } else {
            ft.attach(fragment);
        }
    }

    @Override
    public void onTabReselected(Tab tab, android.app.FragmentTransaction ft){
        // TODO Auto-generated method stub
    }

    @Override
    public void onTabUnselected(Tab tab, android.app.FragmentTransaction ft){
        // TODO Auto-generated method stub  
    }
}

通过删除android.app.FragmentTransaction ft",将其替换为FragmentTransaction ft",问题就解决了.然后新的问题出现了:

By removing "android.app.FragmentTransaction ft", replacing it with just "FragmentTransaction ft", the problem goes awawy. Then new problems arise:

TabListener 类型的 onTabReselected(ActionBar.Tab, FragmentTransaction) 方法必须重写或实现超类型方法 TabListener.java

The method onTabReselected(ActionBar.Tab, FragmentTransaction) of type TabListener must override or implement a supertype method TabListener.java

TabListener 类型的 onTabSelected(ActionBar.Tab, FragmentTransaction) 方法必须重写或实现超类型方法 TabListener.java

The method onTabSelected(ActionBar.Tab, FragmentTransaction) of type TabListener must override or implement a supertype method TabListener.java

TabListener 类型的 onTabUnselected(ActionBar.Tab, FragmentTransaction) 方法必须重写或实现超类型方法 TabListener.java

The method onTabUnselected(ActionBar.Tab, FragmentTransaction) of type TabListener must override or implement a supertype method TabListener.java

TabListener 类型必须实现继承的抽象方法 ActionBar.TabListener.onTabReselected(ActionBar.Tab, FragmentTransaction) TabListener.java

The type TabListener must implement the inherited abstract method ActionBar.TabListener.onTabReselected(ActionBar.Tab, FragmentTransaction) TabListener.java

TabListener 类型必须实现继承的抽象方法 ActionBar.TabListener.onTabSelected(ActionBar.Tab, FragmentTransaction) TabListener.java

The type TabListener must implement the inherited abstract method ActionBar.TabListener.onTabSelected(ActionBar.Tab, FragmentTransaction) TabListener.java

TabListener 类型必须实现继承的抽象方法 ActionBar.TabListener.onTabUnselected(ActionBar.Tab, FragmentTransaction) TabListener.java

The type TabListener must implement the inherited abstract method ActionBar.TabListener.onTabUnselected(ActionBar.Tab, FragmentTransaction) TabListener.java

这是怎么回事?

您可能理解,我是 Java 和 Android 开发的新手.我觉得我很接近,但我无法解决这个问题.我不明白为什么它想当我什至没有在任何地方导入 android.app.Fragment 时从 android.support.v4.app.Fragment 转换为 android.app.Fragment.

As you may understand, I'm new to Java and Android development. I feel like I'm pretty close, but I'm not able to solve this problem. I don't understand why it want to "convert from android.support.v4.app.Fragment to android.app.Fragment when I'm not even importing android.app.Fragment anywhere.

我猜这与使用兼容性包有关.(在为最新版本的 SDK 创建应用程序时,我是否必须使用这个包?)

I guess it's related to using the compatibility package. (Do I have to use this package at all when creating an app for the newest version of the SDK?)

推荐答案

尝试使用 getSupportFragmentManager() 代替 getFragmentManager()

相关文章