tyb
tyb

Reputation: 4749

Fixing error on Android fragments example

I'm building the example shown in this tutorial:

http://thepseudocoder.wordpress.com/2011/10/04/android-tabs-the-fragment-way/

And I'm get some errors in this part of the code:

    private static void addTab(TabsFragmentActivity activity, TabHost tabHost, TabHost.TabSpec tabSpec, TabInfo tabInfo) {
    // Attach a Tab view factory to the spec
    tabSpec.setContent(activity.new TabFactory(activity));
    String tag = tabSpec.getTag();

    // Check to see if we already have a fragment for this tab, probably
    // from a previously saved state.  If so, deactivate it, because our
    // initial state is that a tab isn't shown.
    tabInfo.fragment = activity.getSupportFragmentManager().findFragmentByTag(tag);
    if (tabInfo.fragment != null && !tabInfo.fragment.isDetached()) {
        FragmentTransaction ft =     activity.getSupportFragmentManager().beginTransaction();
        ft.detach(tabInfo.fragment);
        ft.commit();
        activity.getSupportFragmentManager().executePendingTransactions();
    }

    tabHost.addTab(tabSpec);
}

/** (non-Javadoc)
 * @see android.widget.TabHost.OnTabChangeListener#onTabChanged(java.lang.String)
 */
public void onTabChanged(String tag) {
    TabInfo newTab = this.mapTabInfo.get(tag);
   if (mLastTab != newTab) {
        FragmentTransaction ft = this.getSupportFragmentManager().beginTransaction();
        if (mLastTab != null) {
            if (mLastTab.fragment != null) {

                ft.detach(mLastTab.fragment);
            }

       }
        if (newTab != null) {
            if (newTab.fragment == null) {
                newTab.fragment = Fragment.instantiate(this,
                        newTab.clss.getName(), newTab.args);
                ft.add(R.id.realtabcontent, newTab.fragment, newTab.tag);
            } else {
                ft.attach(newTab.fragment);
            }
        }

        mLastTab = newTab;
        ft.commit();
        this.getSupportFragmentManager().executePendingTransactions();
    }
}

}

The errors I'm getting are:

The method isDetached() is undefined for the type Fragment

The method detach(Fragment) is undefined for the type FragmentTransaction

The method attach(Fragment) is undefined for the type FragmentTransaction

Type mismatch: cannot convert from Object to TabsFragmentActivity.TabInfo

Any help on how to fix this would be appreciated. Thanks

Upvotes: 0

Views: 3855

Answers (2)

coquivhdl
coquivhdl

Reputation: 1

Did you write

TabInfo newTab =(TabInfo) this.mapTabInfo.get(tag);

Instead of

TabInfo newTab = this.mapTabInfo.get(tag);

This it is what I did but it didn't work..

Upvotes: 0

Rafael T
Rafael T

Reputation: 15689

It seems like @Ryan Berger suggested that you simply forgot to import android.support.v4.app.Fragment and android.support.v4.app.FragmentTransaction, or that you are missing the whole support-compatibility.jar as reference inside Java-BuildPath/Library Preference

Upvotes: 2

Related Questions