简体   繁体   English

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

[英]Cannot convert from android.support.v4.app.Fragment to android.app.Fragment

I'm doing my first Android app, and wanted to get straight into the ICS API. 我正在做我的第一个Android应用程序,并希望直接进入ICS API。 I have so far created an app using an ActionBar, with swipeable tabs using Viewpager and Fragments. 到目前为止,我已经使用ActionBar创建了一个应用程序,使用Viewpager和Fragments可以使用可滑动的选项卡。

I do however experience some errors that I keep returning to. 然而,我确实经历了一些我一直在回归的错误。

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". 根据我如何实现它,它总是回到“类型不匹配”错误:“无法从android.support.v4.app.Fragment转换为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,FragmentActivity和我的两个片段中仅使用android.support.v4.app.Fragment时出现此错误。

The error occurs in my TabListener: 我的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  
    }
}

By removing "android.app.FragmentTransaction ft", replacing it with just "FragmentTransaction ft", the problem goes awawy. 通过删除“android.app.FragmentTransaction ft”,将其替换为“FragmentTransaction ft”,问题变得非常糟糕。 Then new problems arise: 然后出现新问题:

The method onTabReselected(ActionBar.Tab, FragmentTransaction) of type TabListener must override or implement a supertype method TabListener.java TabListener类型的onTabReselected(ActionBar.Tab,FragmentTransaction)方法必须覆盖或实现超类型方法TabListener.java

The method onTabSelected(ActionBar.Tab, FragmentTransaction) of type TabListener must override or implement a supertype method TabListener.java TabListener类型的onTabSelected(ActionBar.Tab,FragmentTransaction)方法必须覆盖或实现超类型方法TabListener.java

The method onTabUnselected(ActionBar.Tab, FragmentTransaction) of type TabListener must override or implement a supertype method TabListener.java TabListener类型的onTabUnselected(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.onTabReselected(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.onTabSelected(ActionBar.Tab,FragmentTransaction)TabListener.java

The type TabListener must implement the inherited abstract method ActionBar.TabListener.onTabUnselected(ActionBar.Tab, FragmentTransaction) TabListener.java TabListener类型必须实现继承的抽象方法ActionBar.TabListener.onTabUnselected(ActionBar.Tab,FragmentTransaction)TabListener.java

Whats going on here? 这里发生了什么?

As you may understand, I'm new to Java and Android development. 您可能已经了解,我是Java和Android开发的新手。 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. 我不明白为什么它要“从android.support.v4.app.Fragment转换为android.app.Fragment,当我甚至没有在任何地方导入android.app.Fragment时。

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?) (在为最新版本的SDK创建应用程序时,我是否必须使用此软件包?)

尝试使用getSupportFragmentManager()而不是getFragmentManager()

Whats going on here? 这里发生了什么?

While the Android Support package gives you a backwards-compatible Fragment implementation, the ActionBar is not part of the Android Support package. 虽然Android支持包为您提供了向后兼容的Fragment实现,但ActionBar 不是 Android支持包的一部分。 Hence, ActionBar.TabListener is expecting native API Level 11 Fragment objects. 因此, ActionBar.TabListener期待本机API Level 11 Fragment对象。 Consider using ActionBarSherlock to have both an action bar and Android Support fragments. 考虑使用ActionBarSherlock同时拥有操作栏和Android支持片段。

but then I'm left with another problem in my FragmentPagerAdapter 但是我在FragmentPagerAdapter中遇到了另一个问题

The FragmentPagerAdapter in the Android Support package is a bit messy -- it wants API Level 11 Fragment objects, not Android Support Fragment objects. Android支持包中的FragmentPagerAdapter有点乱 - 它需要API Level 11 Fragment对象,而不是Android Support Fragment对象。 However, you can clone the source to FragmentPagerAdapter (source is in your SDK) and create your own implementation that uses the support.v4 flavor of Fragment and kin. 但是,您可以将源克隆到FragmentPagerAdapter (源代码在SDK中)并创建自己的实现,该实现使用Fragment和kin的support.v4风格。

I know that it has been too late to answer this question but it might help someone with the same problem. 我知道现在回答这个问题为时已晚,但它可能会帮助有同样问题的人。

Go to your java folder and click on your fragment's activity. 转到java文件夹,然后单击片段的活动。

In the imports, replace import android.app.Fragment; 在导入中,替换import android.app.Fragment; with

import android.support.v4.app.Fragment;

Keep the code in the MainActivity intact and this should help resolve the issue. 保持MainActivity中的代码不变,这应该有助于解决问题。

Note: If it doesn't work at once, don't worry. 注意:如果它不能立即工作,请不要担心。 Build > Rebuild project . 构建>重建项目

This solution works for me 这个解决方案适合我

replace 更换

public class MyFragment extends Fragment{
}

with

public class MyFragment extends android.support.v4.app.Fragment{
}

and also replace import 并取代进口

import android.app.Fragment;

with

import android.support.v4.app.Fragment;

I've had the same problem yesterday. 我昨天遇到了同样的问题。

There is a really nice page by Samsung that covers ActionBarSherlock. 三星有一个非常好的页面 ,涵盖了ActionBarSherlock。 Check if you use one of the imports/classes/methods on the left and replace them by the imports/classes/methods on the right. 检查是否使用左侧的导入/类/方法之一,并用右侧的imports / classes / methods替换它们。

I had the same problem. 我有同样的问题。 Solved it by changing the interface from implements ActionBar.TabListener to implements TabListener and then implement the methods inside this interface. 通过从implements ActionBar.TabListener更改接口来implements TabListener ,然后在此接口中实现方法来解决它。 Its also mentioned the error you have mentioned. 它还提到了你提到的错误。

look here: fragmentTransaction.add(R.id.main_container, new HomeFragment()); 看这里: fragmentTransaction.add(R.id.main_container, new HomeFragment()); you add the fragment ,but follow,you use replace() method ,so you should use replace instead of add() 你添加了片段,但是你可以使用replace()方法,所以你应该使用replace而不是add()

You can remove the support package, and that should solve your problem. 您可以删除支持包,这应该可以解决您的问题。 It is only needed when you need functions from Android 3.0 and above in apps for earlier versions. 只有在早期版本的应用程序中需要Android 3.0及更高版本的功能时才需要它。
In your case you get both the default Fragments from ICS, and the Fragments from the support package, and if you happen to get objects from the different packages they will not work together. 在您的情况下,您可以从ICS获取默认碎片,从支持包中获取碎片,如果您碰巧从不同的包中获取对象,它们将无法一起工作。

Short version; 精简版; You use either an api level above Honecomb or the support package, not both. 您可以使用Honecomb之上的api级别支持包,而不是两者。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型不匹配:无法从android.support.v4.app.Fragment转换为android.app.Fragment - Type mismatch: cannot convert from android.support.v4.app.Fragment to android.app.Fragment 错误:(31,40)错误:不兼容的类型:android.support.v4.app.Fragment无法转换为android.app.Fragment - Error:(31, 40) error: incompatible types: android.support.v4.app.Fragment cannot be converted to android.app.Fragment 在实现FragmentTabHost时将android.support.v4.app.Fragment强制转换为android.app.Fragment的可能性 - Possibility of casting android.support.v4.app.Fragment to android.app.Fragment when implementing a FragmentTabHost 在同一活动中使用android.support.v4.app.Fragment和android.app.Fragment - Use android.support.v4.app.Fragment and android.app.Fragment in same activity 使用backstack时android.app.Fragment和android.support.v4.app.Fragment的不同行为 - Different behavior of android.app.Fragment and android.support.v4.app.Fragment while using backstack 有没有一种方法可以将片段转换为android.support.v4.app.Fragment - Is there a way to convert fragment to android.support.v4.app.Fragment 必需的android.support.v4.app.fragment - required android.support.v4.app.fragment 覆盖 android.support.v4.app.fragment 中已弃用的方法 - Override deprecated method in android.support.v4.app.fragment com.example.main无法转换为android.support.v4.app.Fragment - com.example.main cannot be cast to android.support.v4.app.Fragment android.support.v4.app.fragment和androidx.fragment.app.FragmentActivity有什么区别 - What is the difference between android.support.v4.app.fragment and androidx.fragment.app.FragmentActivity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM