java.lang.IllegalStateException: Action Bar Tab 必须有一个回调

我正在尝试向我的应用程序添加一个带有4个选项卡。我试图从SherlockActivitySherlockFragmentActivity扩展,但我仍然得到这个例外:ActionBarSherlockthen

java.lang.IllegalStateException: Action Bar Tab 必须有一个回调。

    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

    }
}

答案 1

在将 ActionBar.TabListener 添加到操作栏之前,必须在选项卡上注册该列表。

在您的情况下:

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

否则,我建议您在自己的类中实现您的侦听器,以便您的活动不会失去其目的,并且也易于简化和可读。


答案 2

您可以重复选项卡侦听器,如下所示

    ActionBar.Tab tab1 = actionBar.newTab();
    tab1.setText("Tab 1");
    tab1.setTabListener(this);

    ActionBar.Tab tab2 = actionBar.newTab();
    tab2.setText("Tab 2");
    tab2.setTabListener(this);

    ActionBar.Tab tab3 = actionBar.newTab();
    tab3.setText("Tab 3");
    tab2.setTabListener(this); **// this is must be tab3 not tab2**

推荐