user1662302
user1662302

Reputation:

Action Bar Tab must have a Callback

I'm trying to implement an ActionBar in my application. My code is fine but when I try to load the project it gives me this error :

Caused by: java.lang.IllegalStateException: Action Bar Tab must have a Callback, and forces closure

How can I implement a call back?

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;

public class MainResource extends Activity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        final ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        Tab tabA = actionBar.newTab();
        tabA.setText("Tab A");
        tabA.setTabListener(new TabListener<Categories>(this, "Tag A", Categories.class));
        actionBar.addTab(tabA);

        Tab tabB = actionBar.newTab();
        tabB.setText("Tab B");
        tabB.setTabListener(new TabListener<Favourites>(this, "Tag B", Favourites.class));
        actionBar.addTab(tabB);

        Tab tabC = actionBar.newTab();
        tabC.setText("Tab C");
        tabC.setTabListener(new TabListener<ThisWeek>(this, "Tag C", ThisWeek.class));
        actionBar.addTab(tabC);

        Tab tabD = actionBar.newTab();
        tabC.setText("Tab D");
        tabC.setTabListener(new TabListener<Estore>(this, "Tag D", Estore.class));
        actionBar.addTab(tabD);


        if (savedInstanceState != null) {
            int savedIndex = savedInstanceState.getInt("SAVED_INDEX");
            getActionBar().setSelectedNavigationItem(savedIndex);
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        // TODO Auto-generated method stub
        super.onSaveInstanceState(outState);
        outState.putInt("SAVED_INDEX", getActionBar().getSelectedNavigationIndex());
    }


    public static class TabListener<T extends Fragment> implements ActionBar.TabListener{
        private final Activity myActivity;
        private final String myTag;
        private final Class<T> myClass;


        public TabListener(Activity activity, String tag, Class<T> cls) {
            myActivity = activity;
            myTag = tag;
            myClass = cls;
        }

        @Override
        public void onTabSelected(Tab tab, FragmentTransaction ft) {

            Fragment myFragment = myActivity.getFragmentManager().findFragmentByTag(myTag);

           // Check if the fragment is already initialized
           if (myFragment == null) {
               // If not, instantiate and add it to the activity
               myFragment = Fragment.instantiate(myActivity, myClass.getName());
               ft.add(android.R.id.content, myFragment, myTag);
           } else {
               // If it exists, simply attach it in order to show it
               ft.attach(myFragment);
           }
        }

        @Override
        public void onTabUnselected(Tab tab, FragmentTransaction ft) {
            Fragment myFragment = myActivity.getFragmentManager().findFragmentByTag(myTag);
            if (myFragment != null) {
                // Detach the fragment, because another one is being attached
            ft.detach(myFragment);
            }
        }

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

Upvotes: 3

Views: 6503

Answers (2)

Kuldeep Sakhiya
Kuldeep Sakhiya

Reputation: 3252

Tab tabD = actionBar.newTab();

    tabC.setText("Tab D");

    tabC.setTabListener(new TabListener<Estore>(this, "Tag D", Estore.class));

Change tabC to tabD and problem will be solved.

Upvotes: 0

Rodion Altshuler
Rodion Altshuler

Reputation: 1783

You're setting "this" (current activity object) as a listener:

tabA.setTabListener(new TabListener<Categories>(this, "Tag A", Categories.class));

To be a listener, activity should implement proper interface. So:

  • Declare your activity in this way:
    public class MainResource extends Activity implements TabListener
  • Implement methods of TabListener interface in activity (your IDE will help you by advising "implement methods...")

Maybe there are some more issues with your code, but You need to assign a right object as a TabListener for sure.

Upvotes: 1

Related Questions