Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
263 views
in Technique[技术] by (71.8m points)

java - 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. 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.

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.

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  
    }
}

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

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

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

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

The type TabListener must implement the inherited abstract method 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

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

Whats going on here?

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.

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?)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try to use getSupportFragmentManager() instead getFragmentManager()


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...