18

我正在尝试向ActionBarSherlock我的应用程序添加一个带有 4 个选项卡的选项卡。我试图从 SherlockActivity thenSherlockFragmentActivity` 扩展,但我仍然得到这个异常:

java.lang.IllegalStateException:操作栏选项卡必须有回调。

    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

    }
}
4

2 回答 2

35

您必须先在 Tab 上注册 ActionBar.TabListener,然后才能将其添加到 actionBar。

在你的情况下:

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

否则我会建议你在他们自己的类中实现你的监听器,这样你的 Activity 就不会失去它的目的,并且也很容易理解和阅读。

于 2012-08-09T14:05:45.697 回答
0

您可以像这样重复选项卡侦听器

    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**
于 2014-10-22T21:46:29.653 回答