Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.IllegalStateException: Action Bar Tab must have a Callback

I am trying to add an ActionBarSherlock with 4 tabs to my application. I tried to extend from SherlockActivitythenSherlockFragmentActivity`, but I still get this Exception:

java.lang.IllegalStateException: Action Bar Tab must have a Callback.

    import java.io.IOException;
    import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.json.JSONArray;
import org.json.JSONObject;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.ActionBar.TabListener;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;

import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;
import twitter4j.auth.RequestToken;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.preference.PreferenceManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class HomeActivity extends SherlockFragmentActivity implements OnClickListener, OnItemClickListener, TabListener {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.homelayout);
        try{

            ActionBar actionBar = getSupportActionBar();
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

            Tab homeTab = actionBar.newTab();
            homeTab.setIcon(R.drawable.tab_home);
            Tab exploreTab = actionBar.newTab();
            exploreTab.setIcon(R.drawable.tab_explore);
            Tab leaderBoardTab = actionBar.newTab();
            leaderBoardTab.setIcon(R.drawable.tab_leaderboard);
            Tab profileTab = actionBar.newTab();
            profileTab.setIcon(R.drawable.tab_profile);

            actionBar.addTab(homeTab);
            actionBar.addTab(exploreTab);
            actionBar.addTab(leaderBoardTab);
            actionBar.addTab(profileTab);
                     }catch(Exception ex){

            Log.e("error from onCreate" , ex.toString());
            Log.e("error from onCreate" , ex.getStackTrace().toString());
            }

               }

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

    }



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

    }



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

    }
}
like image 921
Osama Abulkhair Avatar asked Aug 07 '12 13:08

Osama Abulkhair


1 Answers

You must register an ActionBar.TabListener on a Tab before adding it to the actionBar.

In your case:

Tab homeTab = actionBar.newTab();
homeTab.setTabListener(this) //'this' because your activity implements a TabListener
...
actionBar.addTab(homeTab);

Otherwise I would recommend you to implement your listeners in their own classes so that your Activity doesn't loose its purpose and is also easily understendable and readable.

like image 99
GMan Avatar answered Sep 17 '22 15:09

GMan