本文介绍了不能从android.support.v4.app.Fragment转换为android.app.Fragment的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做我的第一个Android应用程序,并希望得到直接进入ICS API。我已创建至今使用动作条的应用程序,使用Viewpager和片段swipeable标签。

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。我曾尝试删除所有进口引用要么,并出现这个错误时,我只用and​​roid.support.v4.app.Fragment在TabListener,FragmentActivity和我的两个片段。

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英尺,只有FragmentTransaction英尺取而代之,问题消失awawy。然后,新的问题就出来了:

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

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

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

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

onTabUnselected型TabListener的(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的发展。我觉得我pretty的接近,但我没有能够解决这个问题。我不明白为什么它想从android.support.v4.app.Fragment转换为android.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()

这篇关于不能从android.support.v4.app.Fragment转换为android.app.Fragment的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-07 04:17